forked from Xrysnow/lstgx_LuaCore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjit_test.lua
110 lines (93 loc) · 2.09 KB
/
jit_test.lua
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
---
--- jit_test.lua
---
--- Copyright (C) 2018-2020 Xrysnow. All rights reserved.
---
--jit.opt.start('sizemcode=1024', 'maxmcode=1024')
--for i = 1, 100 do
--end
local SystemLog = lstg.Print
SystemLog("jit version: " .. jit.version)
local status = { jit.status() }
status[1] = tostring(status[1])
SystemLog('jit status: ' .. table.concat(status, ', '))
SystemLog(string.format('lua memory: %.1fKB', collectgarbage('count')))
local osname = lstg.GetPlatform()
local is_mobile = osname == 'android' or osname == 'ios'
local N = is_mobile and 5e3 or 5e5
local ret = {}
local mode = {
off = function()
jit.off()
end,
on0 = function()
jit.on()
jit.opt.start(0)
end,
on1 = function()
jit.on()
jit.opt.start(1)
end,
on2 = function()
jit.on()
jit.opt.start(2)
end,
on3 = function()
jit.on()
jit.opt.start(3)
end,
}
mode.off()
local sw = lstg.StopWatch()
local n = 0
local function test()
local t1, t2, t3
local test1 = function()
local t = { 1, 2, 3, 4, 5 }
return t[5]
end
local test2 = function()
return {}
end
local test3 = function(n)
return ((n - 10) * 2 + 20) / 2
end
sw:get()
for i = 1, N * 0.1 do
test1()
end
t1 = sw:get()
for i = 1, N * 1 do
test2()
end
t2 = sw:get()
for i = 1, N * 1 do
n = test3(n)
end
t3 = sw:get()
return t1, t2, t3, t1 + t2 + t3
end
ret.off = { test() }
for i = 0, 3 do
mode['on' .. i]()
ret['on' .. i] = { test() }
end
local info = 'jit test result (ms):'
local ffmt = '%6.2f'
local fmt = ' %s: ' .. ffmt .. string.rep(', ' .. ffmt, #ret.on3 - 2) .. ' | ' .. ffmt
local total = {}
local i_n = #ret.on3
for k, v in pairs(mode) do
local str = string.format(fmt, k, unpack(ret[k]))
table.insert(total, { k, ret[k][i_n], str })
end
table.sort(total, function(a, b)
return a[2] < b[2]
end)
for i, v in ipairs(total) do
info = info .. '\n' .. v[3]
end
SystemLog(info)
local m = total[1][1]
mode[m]()
SystemLog('switch to jit mode: ' .. m)