Luajit性能
硬件规格:ecs.t5-lc1m1.small
CPU: Intel(R) Xeon(R) Platinum 8163 CPU @ 2.50GHz
一、预先生成数组
耗时由1.5秒提升到0.9秒
实验组
resty -e '
local begin = ngx.now()
local t = {}
for i=1,10000000 do
table.insert(t, i)
end
ngx.update_time()
print(ngx.now() - begin)
'
用时:1.5160000324249
对照组
resty -e '
local new_table = require("table.new")
local begin = ngx.now()
local t = new_table(10000000, 0)
for i=1,10000000 do
table.insert(t, i)
end
ngx.update_time()
print(ngx.now() - begin)
'
用时:0.94700002670288
二、自己计算table下标
耗时由0.93秒提升到0.03秒
实验组
resty -e '
local new_table = require("table.new")
local begin = ngx.now()
local t = new_table(10000000, 0)
for i=1,10000000 do
table.insert(t, i)
end
ngx.update_time()
print(ngx.now() - begin)
'
用时:0.93299984931946
对照组
resty -e '
local new_table = require("table.new")
local begin = ngx.now()
local t = new_table(10000000, 0)
for i=1,1000000 do
t[i] = i
end
ngx.update_time()
print(ngx.now() - begin)
'
用时:0.032000064849854
三、循环使用table
耗时由0.06秒提升到0.035秒
实验组
resty -e '
local new_table = require("table.new")
local begin = ngx.now()
local t1 = new_table(10000000, 0)
for i=1,1000000 do
t1[i] = i
end
local t2 = new_table(10000000, 0)
for i=1,1000000 do
t2[i] = i
end
ngx.update_time()
print(ngx.now() - begin)
'
用时:0.06600022315979
实验组
resty -e '
local new_table = require("table.new")
local begin = ngx.now()
local t = new_table(10000000, 0)
for i=1,1000000 do
t[i] = i
end
for i=1,1000000 do
t[i] = i
end
ngx.update_time()
print(ngx.now() - begin)
'
用时:0.035000085830688
四、使用jit优化函数
耗时由0.9秒提升到0.75秒
实验组
resty -e '
local new_table = require("table.new")
local t = new_table(1000000, 0)
for i=1,1000000 do
t[tostring(i)] = i
end
local begin = ngx.now()
for k,v in pairs(t) do
t[k] = "ok"
end
ngx.update_time()
print(ngx.now() - begin)
'
用时:0.90300011634827
对照组
resty -e '
local new_table = require("table.new")
local t = new_table(1000000, 0)
for i=1,1000000 do
t[tostring(i)] = i
end
local begin = ngx.now()
for i,v in ipairs(t) do
t[i] = 1
end
ngx.update_time()
print(ngx.now() - begin)
'
用时:0.75699996948242