Unity中SendMessage和Delegate效率比较
网上直接搜的代码。需要的使用也简单,所以就不过多说明。
但是网上都说,他们之间的差距,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));
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
二、说明
代码基本没有做更改,删掉了没用的函数。也名字也带着呢!!在此表示感谢!!!
但说道具体测试,要每次都需要重新开始,才能计时。要不就会有问题,后测试的就会产生多次循环。
三、效率比较
系统配置:”Win7 64位,i7处理器 16G内存,英伟达660显卡”
对比为倍数关系!!也就说,随着次数增加,效率差距越来越大!!!
Unity中SendMessage和Delegate效率比较的更多相关文章
- 【Unity3D技巧】在Unity中使用事件/委托机制(event/delegate)进行GameObject之间的通信 (二) : 引入中间层NotificationCenter
作者:王选易,出处:http://www.cnblogs.com/neverdie/ 欢迎转载,也请保留这段声明.如果你喜欢这篇文章,请点[推荐].谢谢! 一对多的观察者模式机制有什么缺点? 想要查看 ...
- 【《Effective C#》提炼总结】提高Unity中C#代码质量的21条准则
作者:Williammao, 腾讯移动客户端开发工程师 商业转载请联系腾讯WeTest获得授权,非商业转载请注明出处. 原文链接:http://wetest.qq.com/lab/view/290.h ...
- 【《Effective C#》提炼总结】提高Unity中C#代码质量的22条准则
引言 原则1尽可能地使用属性而不是可直接访问的数据成员 原则2偏向于使用运行时常量而不是编译时常量 原则3 推荐使用is 或as操作符而不是强制类型转换 原则4 推荐使用条件属性而不是if条件编译 原 ...
- 【转】Effective C#观后感之提高Unity中C#代码质量的21条准则
转自:http://blog.csdn.net/swj524152416/article/details/75418162 我们知道,在C++领域,作为进阶阅读材料,必看的书是<Effectiv ...
- Unity中使用Attribute
Attribute是c#的语言特性 msdn说明如下: The Attribute class associates predefined system information or user-def ...
- Unity中使用WebView
Unity中使用WebView @(设计) 需求,最近游戏中需要引入H5直播页面和更新比较频繁的赛事页面,需求包括:加密传参数.和Unity交互,在Unity框架下其实有几种方案: 内置函数Appli ...
- 【原创翻译】初识Unity中的Compute Shader
一直以来都想试着自己翻译一些东西,现在发现翻译真的很不容易,如果你直接把作者的原文按照英文的思维翻译过来,你会发现中国人读起来很是别扭,但是如果你想完全利用中国人的语言方式来翻译,又怕自己理解的不到位 ...
- WP8:在Unity中使用OpenXLive
Unity 4.2正式版开始添加了对Windows 8.Windows Phone 8等其他平台的支持,而且开发者可以免费使用Unity引擎来开发游戏了.而作为Windows Phone和Window ...
- Unity 中 使用c#线程
使用条件 天下没有免费的午餐,在我使用unity的那一刻,我就感觉到不自在,因为开源所以不知道底层实现,如果只是简单的做点简单游戏,那就无所谓的了,但真正用到实际地方的时候,就会发现一个挨着一个坑 ...
随机推荐
- hdu1495(经典bfs,平分水问题)
思路:搜索题,第一次做这种类型的题目吧,一开始表示不怎么明白题意所说的东东.其实就是要你判断可乐能不能被平分........ 有六种状态,从a瓶到b瓶,a-->c b-->a b- ...
- POj 1753--Flip Game(位运算+BFS)
Flip Game Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 30669 Accepted: 13345 Descr ...
- JavaScrip——练习(做悬浮框再进一步:悬浮窗后缀悬浮窗——用this.className)
对悬浮窗进一步改进: 用this.className 可以省略script <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitio ...
- eclipse安装使用教程
eclipse安装使用教程 很多人都知道要用eclipse来做java开发,但很多的新手朋友却不知道怎么下载和安装eclipse. 下面给你介绍一下怎么下载和安装eclipse来用于自己的学习或者项目 ...
- poj3208 Apocalypse Someday 数位dp+二分 求第K(K <= 5*107)个有连续3个6的数。
/** 题目:poj3208 Apocalypse Someday 链接:http://poj.org/problem?id=3208 题意:求第K(K <= 5*107)个有连续3个6的数. ...
- 系统优化 /etc/sysctl.conf
# sysctl settings are defined through files in # /usr/lib/sysctl.d/, /run/sysctl.d/, and /etc/sysctl ...
- 透明遮罩图层VS高斯模糊滤镜 效果分析
前端流行布局中最常见的弹出图层有popup, 对话框, tooltip等, 他们都使用了新的图层,但是实现办法各不相同, 有 的是通过半通明的黑白图层实现的, 有的是通过滤镜实现的, 我们来研究一下两 ...
- *.ashx一般处理程序不能访问Session值的解决方法
<%@ WebHandler Language="C#" Class="productHandler" %> using System; using ...
- Qt 事件过滤器
Qt创建了QEvent事件对象之后,会调用QObject的event()函数做事件的分发.有时候,你可能需要在调用event()函数之前做一些另外的操作,比如,对话框上某些组件可能并不需要响应回车按下 ...
- 修改ES分片规则
转自:http://my.oschina.net/crxy/blog/422287?p=1 Es查询的时候默认是随机从一些分片中查询数据,可以通过配置让es从某些分片中查询数据 1:_local 指查 ...