collectgarbage("stop")
local pos = {0, 2^22, 2^42, 2^46, 2^50}
local q, s, e, we
local head, tail
for i, p in ipairs(pos) do
s = os.clock()
q = {}
for i = p + 1, p + 1000 do
q[i] = 1
end
e = os.clock()
print(p, ":", e - s)
head, tail = p + 1, p + 1000
-- walk 1000000
for i = tail + 1, p + 1000000 do
tail = i
q[i], q[head] = q[head], nil
head = head + 1
end
we = os.clock()
print(p, " walk 1000000:", we - e)
end
➜ test luajit array.lua
0 : 6.6e-05
0 walk 1000000: 1.249039
4194304 : 0.00016399999999983
4194304 walk 1000000: 1.677127
4398046511104 : 0.0013109999999998
4398046511104 walk 1000000: 31.822093
70368744177664 : 0.00019900000000206
70368744177664 walk 1000000: 5.764768
1.1258999068426e+15 : 9.2000000002201e-05
1.1258999068426e+15 walk 1000000: 1.604183
➜ test lua array.lua
0 : 3.5e-05
0 walk 1000000: 0.104204
4194304 : 0.00012400000000001
4194304 walk 1000000: 2.055305
4398046511104 : 0.00010500000000002
4398046511104 walk 1000000: 0.090982
70368744177664 : 0.00013700000000005
70368744177664 walk 1000000: 0.093623
1.1258999068426e+15 : 0.00012400000000001
1.1258999068426e+15 walk 1000000: 0.090589
➜ test lua -v
Lua 5.1.5 Copyright (C) 1994-2012 Lua.org, PUC-Rio
➜ test lua5.4 array.lua
0 : 3e-05
0 walk 1000000: 0.051626
4194304.0 : 9.1999999999995e-05
4194304.0 walk 1000000: 0.08214
4398046511104.0 : 0.00010599999999999
4398046511104.0 walk 1000000: 0.082671
70368744177664.0 : 9.1999999999981e-05
70368744177664.0 walk 1000000: 0.081175
1.1258999068426e+15 : 0.00010199999999999
1.1258999068426e+15 walk 1000000: 0.080874
luajit 是最新的luajit 仓库版本, lua5.4 表现非常好, luajit 在 242 起始位的数组移动百万次性能变差. 有谁能解释一下原理吗?