示例工程 08-table-structure

08-table-structure/03-constructor-prealloc/main.lua

lua · 0.6 KB

-- @runtimes lua54 luajit
--- 演示:构造器预分配与逐项插入。
 
local M = {}
 
local N = 1000
 
--- @return table { constructed = integer, inserted = integer, same = boolean }
function M.run(log)
	log = log or print
	local constructed = {}
	for i = 1, N do
		constructed[i] = i
	end
	local inserted = {}
	for i = 1, N do
		table.insert(inserted, i)
	end
	local same = #constructed == #inserted
	log(("constructed=%d inserted=%d same=%s"):format(#constructed, #inserted, tostring(same)))
	return { constructed = #constructed, inserted = #inserted, same = same }
end
 
if not ... then
	M.run()
end
 
return M