💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
本文章由cartzhang编写,转载请注明出处。 所有权利保留。  文章链接: [http://blog.csdn.net/cartzhang/article/details/50686627](http://blog.csdn.net/cartzhang/article/details/50686627)  作者:cartzhang 网上直接搜的代码。需要的使用也简单,所以就不过多说明。  但是网上都说,他们之间的差距,delegate比较快,效果高。怎么个高法呢?还是自己来测试下时间。  故此,  个人之用来比较下时间差别。 ### 一、直接代码 ~~~ using UnityEngine; using System.Collections; /// <summary> /// Delegate basic. /// just test Delegate && SendMessage .. /// /// By Chiuan 2012.8 /// </summary> public class DelegateBasic : MonoBehaviour { static int loopNumber = 5000000; //define my delegate statement. public delegate void MyDelegate(string arg1); //create my delegate object public MyDelegate myDelegate; //need some values to debug time spent. bool isStart; float timeStart; int count; bool isStartSendMessage; // Use this for initialization void Start() { myDelegate += myFunciton1; //myDelegate += myFunciton2; } // Update is called once per frame void Update() { if (isStart) { isStart = false; count = 0; timeStart = Time.realtimeSinceStartup; Debug.Log("Start = " + timeStart); for (int i = 0; i < loopNumber; i++) { if (myDelegate != null) myDelegate(""); } } if (isStartSendMessage) { isStartSendMessage = false; count = 0; timeStart = Time.realtimeSinceStartup; Debug.Log("Start = " + timeStart); for (int i = 0; i < loopNumber; i++) { this.gameObject.SendMessage("myFunciton1", "", SendMessageOptions.DontRequireReceiver); } } } void OnGUI() { if (GUILayout.Button("INVOKE Delegate")) { isStart = true; } if (GUILayout.Button("SendMessage")) { isStartSendMessage = true; } } void myFunciton1(string s) { count++; if (count == loopNumber) { Debug.Log("End = " + Time.realtimeSinceStartup); Debug.Log("Time Spent = " + (Time.realtimeSinceStartup - timeStart)); } } } ~~~ ### 二、说明 代码基本没有做更改,删掉了没用的函数。也名字也带着呢!!在此表示感谢!!! 但说道具体测试,要每次都需要重新开始,才能计时。要不就会有问题,后测试的就会产生多次循环。 ### 三、效率比较 系统配置:”Win7 64位,i7处理器 16G内存,英伟达660显卡” ![](https://box.kancloud.cn/2016-03-09_56dfda8869afc.jpg) 对比为倍数关系!!也就说,随着次数增加,效率差距越来越大!!! ### 相关链接: [https://unity3d.com/cn/learn/tutorials/modules/intermediate/scripting/delegates](https://unity3d.com/cn/learn/tutorials/modules/intermediate/scripting/delegates) [http://www.xuanyusong.com/archives/1895/](http://www.xuanyusong.com/archives/1895/) [http://blog.csdn.net/chiuan/article/details/7918833](http://blog.csdn.net/chiuan/article/details/7918833) 若有问题,请随时联系!!!  再次表示感谢!!!