疑惑合集

Q1. delegate性能比Luafunction好,为什么?

void Start()
{
    LuaManager.GetInstance().Init();
    LuaManager.GetInstance().DoLuaFile("main");
    Stopwatch sw = new Stopwatch();
    sw.Start();
    delegateRunTime();
    long times1 = sw.ElapsedMilliseconds;
    sw.Restart();
    luaFunctionRunTime();
    long times2 = sw.ElapsedMilliseconds;
    sw.Stop();
    UnityEngine.Debug.Log("T1: " + times1 + " T2: " + times2);
}

void delegateRunTime(){
    for(int i=0;i<10000;i++){
        CustomCall1 customCall1 = LuaManager.GetInstance().Global.Get<CustomCall1>("testFunc1");
        customCall1();
    }
}

void luaFunctionRunTime(){
    for(int i=0;i<10000;i++){
        LuaFunction luaFunction= LuaManager.GetInstance().Global.Get<LuaFunction>("testFunc1");
        luaFunction.Call();
    }
}

对于无参无返回的lua函数,输出了下运行时间,发现相差无几(其实也阅读了下源码,也没有发现性能不一样的地方)

是因为delegrate bridge是弱表吗?弱表代表可以被GC回收,但是luafunction不行。

if (delegateType == null)
{
    delegate_bridges[reference] = new WeakReference(bridge);
    return bridge;
}

发表评论

您的邮箱地址不会被公开。 必填项已用 * 标注

大纲

Share the Post:
滚动至顶部