尝试了以下性能测试:
(1)预先生成数组
resty -e '
local begin = ngx.now()
local t = {}
for i = 1,100000000 do
table.insert(t, i)
end
ngx.update_time()
print(ngx.now()-begin)
'
用时:21.62700009346
resty -e '
local new_tab = require "table.new"
local begin = ngx.now()
local t = new_tab(100000000, 0)
for i = 1,100000000 do
table.insert(t, i)
end
ngx.update_time()
print(ngx.now()-begin)
'
用时:7.595999956131
(2)自己计算table下标
--使用insert函数
resty -e '
local new_tab = require "table.new"
local begin = ngx.now()
local t = new_tab(100000000, 0)
for i = 1,100000000 do
table.insert(t, i)
end
ngx.update_time()
print(ngx.now()-begin)
'
用时:7.6459999084473
--自己计算下标
resty -e '
local new_tab = require "table.new"
local begin = ngx.now()
local t = new_tab(100000000, 0)
for i = 1,100000000 do
t[i] = i
end
ngx.update_time()
print(ngx.now()-begin)
'
用时:0.33599996566772
(3)循环使用单个table
--分别建立两个table
resty -e '
local new_tab = require "table.new"
local begin = ngx.now()
local t1 = new_tab(100000000, 0)
for i = 1,100000000 do
t1[i] = i
end
local t2 = new_tab(100000000, 0)
for i = 1,100000000 do
t2[i] = i
end
ngx.update_time()
print(ngx.now()-begin)
'
用时:0.66700005531311
--复用table
resty -e '
local new_tab = require "table.new"
local begin = ngx.now()
local t = new_tab(100000000, 0)
for i = 1,100000000 do
t[i] = i
end
for i = 1,100000000 do
t[i] = i
end
ngx.update_time()
print(ngx.now()-begin)
'
用时:0.40800023078918
展开