This pits various ways to iterate over varargs against each other, primarily `select` vs `ipairs{...}`. This initial version is taken from DarkWiiPlayer.
68 lines
1.1 KiB
Lua
68 lines
1.1 KiB
Lua
local unpack = table.unpack or
|
|
function(tab)
|
|
return table.remove(tab,1), unpack(tab)
|
|
end
|
|
|
|
function try(fnc,n,...)
|
|
name = name or ""
|
|
ts = os.clock()
|
|
for i=1,n do
|
|
fnc(...)
|
|
end
|
|
te = os.clock()
|
|
print(te-ts)
|
|
end
|
|
|
|
for i=1,8 do
|
|
values = {}
|
|
n = 2^i
|
|
print(("Number of elements: %i"):format(n))
|
|
for i=1,n*2 do
|
|
values[#values+1]=i
|
|
end
|
|
n = 2^22 / n
|
|
print(("Number of iterations: %i"):format(n))
|
|
|
|
print("---- STARTING TEST ----")
|
|
|
|
io.write "numeric for: "
|
|
try(function(...)
|
|
args = {...}
|
|
for i=1,#args do
|
|
a = args[i]
|
|
end
|
|
end, n, unpack(values))
|
|
|
|
io.write "ipairs{...}: "
|
|
try(function(...)
|
|
args = {...}
|
|
for key, value in ipairs(args) do
|
|
a = value
|
|
end
|
|
end, n, unpack(values))
|
|
|
|
io.write "select : "
|
|
try(function(...)
|
|
for i=1,select("#",...) do
|
|
a = select(i,...)
|
|
end
|
|
end, n, unpack(values))
|
|
|
|
---[[
|
|
io.write "combination: "
|
|
try(function(...)
|
|
n_args = select("#",...)
|
|
if n_args<=16 then
|
|
for i=1,n_args do
|
|
a = select(i,...)
|
|
end
|
|
else
|
|
args = {...}
|
|
for key, value in ipairs(args) do
|
|
a = value
|
|
end
|
|
end
|
|
end, n, unpack(values))
|
|
--]]
|
|
print()
|
|
end
|