Unity脚本效率评测
对SLua、Tolua、XLua和ILRuntime四个脚本插件进行效率测试,对框架脚本进行选型。
本文项目:https://github.com/cateatcatx/UnityScriptPTest
tolua:https://github.com/topameng/tolua
slua:https://github.com/pangweiwei/slua
xlua:https://github.com/Tencent/xLua
ILRuntime:https://github.com/Ourpalm/ILRuntime
用例版本
Unity5.6.0p3
SLua 1.3.2
Tolua# github 2017/4/25 19:08:37
XLua 2.1.7
ILRuntime 1.11
Lua: luajit-2.1.0-beta2
测试环境
Smartian T2、Win7(64bit)
实验方案
总共设计了17个Test,分别从以下3个方面来考察脚本效率(JIT和非JIT),实验结果取10次的平均值,时间单位为ms。通过实验数据简单分析原因(Lua插件会横向对比,ILRuntime会单独考虑,因为毕竟C#和Lua本身差别较大)。
1. Mono -> Script,Mono调用脚本
2. Script -> Mono,脚本调用Mono
3. Script自身,脚本自身执行效率
Mono -> Script
Test11 | Test12 | Test13 | Test14 | Test15 | Test16 | Sum | |
---|---|---|---|---|---|---|---|
Tolua | 186 | 449 | 598 | 407 | 577 | 759 | 2978 |
SLua | 315 | 751 | 901 | 757 | 1253 | 1883 | 5863 |
XLua | 145 | 924 | 1010 | 573 | 1507 | 1929 | 6091 |
ILRuntime | 711 | 422 | 368 | 379 | 397 | 393 | 2672 |
Tolua(JIT) | 168 | 454 | 592 | 416 | 578 | 826 | 3037 |
SLua(JIT) | 384 | 842 | 956 | 824 | 1328 | 3439 | 7775 |
XLua(JIT) | 189 | 957 | 1047 | 608 | 1540 | 1700 | 6043 |
ILRuntime(JIT) | 1232 | 892 | 903 | 969 | 1175 | 1102 | 6275 |
Lua分析:
-- Test11
function EmptyFunc()
_V0 = _V0 + 1
end
_V0 = 1 -- Test12
_V1 = "12345" -- Test13
_V2 = GameObject.New() -- Test14
_V3 = Vector3.New(1, 2, 3) -- Test15
_V4 = {
1, 2, 3} -- Test16
Test11为Mono调用脚本中空方法,Test12~16为测试Mono到脚本中取变量(ILRuntime代码类似,访问类的static函数与变量)。ILRuntime因为没有变量类型的转换所以效率最为优秀(JIT模式下使用的是Mono的反射取值所以会更慢,ILRuntime内部可能对类型变量有缓存