Unity交互性能和Lua的对比

网络上有一篇主要关于Unity交互的Lua方案的性能对比测试。
https://zhuanlan.zhihu.com/p/22673880

AppleJuice也进行了一次此测试。测试平台为手机 魅族Note5。对比对象为SLUA 1.5.5。双方均使用il2cpp打包。

AppleJuice是C#代码,因此在UnityEditor中测试结果不准确,需要发布到手机实测,或者发布为PC的exe,才能实际测试性能。

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
package 
{
import flash.system.System;
import system.DateTime;
import system.TimeSpan;
import unityengine.GameObject;
import unityengine.Transform;
import unityengine.Vector3;
/**
* ...
* @author
*/
public final class Perf
{
var cube:GameObject = new GameObject("Script cube");
public function Perf()
{

}

public function Test1():void
{
var d:DateTime = DateTime.now;

var transform:Transform = cube.transform;
for (var i:int = 0; i < 200000 ; i++)
{
transform.position = transform.position;
}
trace( TimeSpan (DateTime.now-d).totalMilliseconds );
}

public function Test2():void
{
var d:DateTime = DateTime.now;

var transform:Transform = cube.transform;
for (var i:int = 0; i < 200000 ; i++)
{
transform.rotate____(Vector3.up, 90);
}
trace( TimeSpan (DateTime.now-d).totalMilliseconds );
}

public function Test3():void
{
var d:DateTime = DateTime.now;


for (var i:int = 0; i < 2000000 ; i++)
{
var v:Vector3 =new Vector3(i,i,i)
Vector3.normalize(v)
}
trace( TimeSpan (DateTime.now-d).totalMilliseconds );
}

public function Test4():void
{
var d:DateTime = DateTime.now;


for (var i:int = 0; i < 200000; i++)
{
var v:GameObject = GameObject.constructor_();
}
trace( TimeSpan (DateTime.now-d).totalMilliseconds );

}


}

}
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
function test1()
local transform = cube.transform
local start = os.clock()
for i=1,200000 do
transform.position=transform.position
end
print("test1/lua " .. (os.clock() - start));
end

function test2()
local transform=cube.transform
local start = os.clock()
for i=1,200000 do
transform:Rotate(Vector3.up, 90)
end
print("test2/lua " .. (os.clock() - start));
end

function test3()
local start = os.clock()
for i=1,2000000 do
local v = Vector3(i,i,i)
Vector3.Normalize(v)
end
print("test3/lua " .. (os.clock() - start));
end

function test4()
local t = cube.transform
local v = Vector3.one
local start = os.clock()
for i=1,200000 do
local v = GameObject()
end
print("test4/lua " .. (os.clock() - start));
end

以下为测试结果 单位为秒。越小越好

  1. test1 slua 1.09 apple juice 1.14
  2. test2 slua 2.57 apple juice 1.84
  3. test3 slua 0.058 apple juice 25.9

    test3 实际上slua并没有和unity交互,因此这个测试不适于评估。

  4. test4 slua 7.25 apple juice 4.128

结果截图说明