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显卡”

数据

对比为倍数关系!!也就说,随着次数增加,效率差距越来越大!!!

原文地址:https://www.cnblogs.com/chenliyang/p/6558680.html