Implement benchmark vararg iteration
This pits various ways to iterate over varargs against each other, primarily `select` vs `ipairs{...}`. This initial version is taken from DarkWiiPlayer.
This commit is contained in:
parent
3728a0e911
commit
f4493fe5ce
3 changed files with 411 additions and 0 deletions
7
vararg-iteration/README.adoc
Normal file
7
vararg-iteration/README.adoc
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
= vararg iteration
|
||||||
|
|
||||||
|
This compares the performance of various approaches to iterate over the arguments of a variadic function.
|
||||||
|
|
||||||
|
== Credits
|
||||||
|
|
||||||
|
Created by https://github.com/DarkWiiPlayer at https://gist.github.com/DarkWiiPlayer/a6496cbce062ebe5d534e4b881d4efef.
|
336
vararg-iteration/results.log
Normal file
336
vararg-iteration/results.log
Normal file
|
@ -0,0 +1,336 @@
|
||||||
|
== SYSTEM INFORMATION
|
||||||
|
CPU: Intel(R) Core(TM) i7-8700K CPU @ 3.70GHz
|
||||||
|
Kernel: 5.19.13-zen1-1-zen
|
||||||
|
|
||||||
|
== TEST RESULTS
|
||||||
|
|
||||||
|
Lua: lua5.1
|
||||||
|
|
||||||
|
Number of elements: 2
|
||||||
|
Number of iterations: 2097152
|
||||||
|
---- STARTING TEST ----
|
||||||
|
numeric for: 0.552162
|
||||||
|
ipairs{...}: 0.690995
|
||||||
|
select : 0.286485
|
||||||
|
combination: 0.25396
|
||||||
|
|
||||||
|
Number of elements: 4
|
||||||
|
Number of iterations: 1048576
|
||||||
|
---- STARTING TEST ----
|
||||||
|
numeric for: 0.417153
|
||||||
|
ipairs{...}: 0.545133
|
||||||
|
select : 0.328147
|
||||||
|
combination: 0.310883
|
||||||
|
|
||||||
|
Number of elements: 8
|
||||||
|
Number of iterations: 524288
|
||||||
|
---- STARTING TEST ----
|
||||||
|
numeric for: 0.348244
|
||||||
|
ipairs{...}: 0.471477
|
||||||
|
select : 0.367715
|
||||||
|
combination: 0.371746
|
||||||
|
|
||||||
|
Number of elements: 16
|
||||||
|
Number of iterations: 262144
|
||||||
|
---- STARTING TEST ----
|
||||||
|
numeric for: 0.302945
|
||||||
|
ipairs{...}: 0.428739
|
||||||
|
select : 0.443048
|
||||||
|
combination: 0.418883
|
||||||
|
|
||||||
|
Number of elements: 32
|
||||||
|
Number of iterations: 131072
|
||||||
|
---- STARTING TEST ----
|
||||||
|
numeric for: 0.288698
|
||||||
|
ipairs{...}: 0.415577
|
||||||
|
select : 0.602379
|
||||||
|
combination: 0.39393
|
||||||
|
|
||||||
|
Number of elements: 64
|
||||||
|
Number of iterations: 65536
|
||||||
|
---- STARTING TEST ----
|
||||||
|
numeric for: 0.267878
|
||||||
|
ipairs{...}: 0.406569
|
||||||
|
select : 0.964695
|
||||||
|
combination: 0.390686
|
||||||
|
|
||||||
|
Number of elements: 128
|
||||||
|
Number of iterations: 32768
|
||||||
|
---- STARTING TEST ----
|
||||||
|
numeric for: 0.254064
|
||||||
|
ipairs{...}: 0.384773
|
||||||
|
select : 1.625094
|
||||||
|
combination: 0.383372
|
||||||
|
|
||||||
|
Number of elements: 256
|
||||||
|
Number of iterations: 16384
|
||||||
|
---- STARTING TEST ----
|
||||||
|
numeric for: 0.247988
|
||||||
|
ipairs{...}: 0.384708
|
||||||
|
select : 2.943422
|
||||||
|
combination: 0.381021
|
||||||
|
|
||||||
|
Lua: lua5.2
|
||||||
|
|
||||||
|
Number of elements: 2
|
||||||
|
Number of iterations: 2097152
|
||||||
|
---- STARTING TEST ----
|
||||||
|
numeric for: 0.560537
|
||||||
|
ipairs{...}: 0.746473
|
||||||
|
select : 0.537075
|
||||||
|
combination: 0.609935
|
||||||
|
|
||||||
|
Number of elements: 4
|
||||||
|
Number of iterations: 1048576
|
||||||
|
---- STARTING TEST ----
|
||||||
|
numeric for: 0.436931
|
||||||
|
ipairs{...}: 0.563225
|
||||||
|
select : 0.487017
|
||||||
|
combination: 0.519111
|
||||||
|
|
||||||
|
Number of elements: 8
|
||||||
|
Number of iterations: 524288
|
||||||
|
---- STARTING TEST ----
|
||||||
|
numeric for: 0.363167
|
||||||
|
ipairs{...}: 0.465143
|
||||||
|
select : 0.474101
|
||||||
|
combination: 0.489039
|
||||||
|
|
||||||
|
Number of elements: 16
|
||||||
|
Number of iterations: 262144
|
||||||
|
---- STARTING TEST ----
|
||||||
|
numeric for: 0.324731
|
||||||
|
ipairs{...}: 0.414151
|
||||||
|
select : 0.530607
|
||||||
|
combination: 0.446816
|
||||||
|
|
||||||
|
Number of elements: 32
|
||||||
|
Number of iterations: 131072
|
||||||
|
---- STARTING TEST ----
|
||||||
|
numeric for: 0.306231
|
||||||
|
ipairs{...}: 0.392634
|
||||||
|
select : 0.661512
|
||||||
|
combination: 0.408606
|
||||||
|
|
||||||
|
Number of elements: 64
|
||||||
|
Number of iterations: 65536
|
||||||
|
---- STARTING TEST ----
|
||||||
|
numeric for: 0.305991
|
||||||
|
ipairs{...}: 0.372077
|
||||||
|
select : 1.049758
|
||||||
|
combination: 0.379606
|
||||||
|
|
||||||
|
Number of elements: 128
|
||||||
|
Number of iterations: 32768
|
||||||
|
---- STARTING TEST ----
|
||||||
|
numeric for: 0.287538
|
||||||
|
ipairs{...}: 0.372301
|
||||||
|
select : 1.77883
|
||||||
|
combination: 0.369424
|
||||||
|
|
||||||
|
Number of elements: 256
|
||||||
|
Number of iterations: 16384
|
||||||
|
---- STARTING TEST ----
|
||||||
|
numeric for: 0.287302
|
||||||
|
ipairs{...}: 0.358031
|
||||||
|
select : 3.178964
|
||||||
|
combination: 0.363598
|
||||||
|
|
||||||
|
Lua: lua5.3
|
||||||
|
|
||||||
|
Number of elements: 2
|
||||||
|
Number of iterations: 2097152
|
||||||
|
---- STARTING TEST ----
|
||||||
|
numeric for: 0.46859
|
||||||
|
ipairs{...}: 0.640256
|
||||||
|
select : 0.437587
|
||||||
|
combination: 0.483519
|
||||||
|
|
||||||
|
Number of elements: 4
|
||||||
|
Number of iterations: 1048576
|
||||||
|
---- STARTING TEST ----
|
||||||
|
numeric for: 0.364446
|
||||||
|
ipairs{...}: 0.466707
|
||||||
|
select : 0.390046
|
||||||
|
combination: 0.423592
|
||||||
|
|
||||||
|
Number of elements: 8
|
||||||
|
Number of iterations: 524288
|
||||||
|
---- STARTING TEST ----
|
||||||
|
numeric for: 0.285324
|
||||||
|
ipairs{...}: 0.36883
|
||||||
|
select : 0.386637
|
||||||
|
combination: 0.402977
|
||||||
|
|
||||||
|
Number of elements: 16
|
||||||
|
Number of iterations: 262144
|
||||||
|
---- STARTING TEST ----
|
||||||
|
numeric for: 0.247901
|
||||||
|
ipairs{...}: 0.323775
|
||||||
|
select : 0.438333
|
||||||
|
combination: 0.341976
|
||||||
|
|
||||||
|
Number of elements: 32
|
||||||
|
Number of iterations: 131072
|
||||||
|
---- STARTING TEST ----
|
||||||
|
numeric for: 0.234862
|
||||||
|
ipairs{...}: 0.302702
|
||||||
|
select : 0.545853
|
||||||
|
combination: 0.317096
|
||||||
|
|
||||||
|
Number of elements: 64
|
||||||
|
Number of iterations: 65536
|
||||||
|
---- STARTING TEST ----
|
||||||
|
numeric for: 0.220949
|
||||||
|
ipairs{...}: 0.290624
|
||||||
|
select : 0.8336
|
||||||
|
combination: 0.292184
|
||||||
|
|
||||||
|
Number of elements: 128
|
||||||
|
Number of iterations: 32768
|
||||||
|
---- STARTING TEST ----
|
||||||
|
numeric for: 0.213136
|
||||||
|
ipairs{...}: 0.28163
|
||||||
|
select : 1.308727
|
||||||
|
combination: 0.279163
|
||||||
|
|
||||||
|
Number of elements: 256
|
||||||
|
Number of iterations: 16384
|
||||||
|
---- STARTING TEST ----
|
||||||
|
numeric for: 0.215232
|
||||||
|
ipairs{...}: 0.272502
|
||||||
|
select : 2.274558
|
||||||
|
combination: 0.273622
|
||||||
|
|
||||||
|
Lua: lua5.4
|
||||||
|
|
||||||
|
Number of elements: 2
|
||||||
|
Number of iterations: 2097152
|
||||||
|
---- STARTING TEST ----
|
||||||
|
numeric for: 0.373074
|
||||||
|
ipairs{...}: 0.512667
|
||||||
|
select : 0.368981
|
||||||
|
combination: 0.426854
|
||||||
|
|
||||||
|
Number of elements: 4
|
||||||
|
Number of iterations: 1048576
|
||||||
|
---- STARTING TEST ----
|
||||||
|
numeric for: 0.330613
|
||||||
|
ipairs{...}: 0.42877
|
||||||
|
select : 0.371128
|
||||||
|
combination: 0.427162
|
||||||
|
|
||||||
|
Number of elements: 8
|
||||||
|
Number of iterations: 524288
|
||||||
|
---- STARTING TEST ----
|
||||||
|
numeric for: 0.218039
|
||||||
|
ipairs{...}: 0.378982
|
||||||
|
select : 0.42071
|
||||||
|
combination: 0.388871
|
||||||
|
|
||||||
|
Number of elements: 16
|
||||||
|
Number of iterations: 262144
|
||||||
|
---- STARTING TEST ----
|
||||||
|
numeric for: 0.207389
|
||||||
|
ipairs{...}: 0.314401
|
||||||
|
select : 0.466114
|
||||||
|
combination: 0.325659
|
||||||
|
|
||||||
|
Number of elements: 32
|
||||||
|
Number of iterations: 131072
|
||||||
|
---- STARTING TEST ----
|
||||||
|
numeric for: 0.203002
|
||||||
|
ipairs{...}: 0.265324
|
||||||
|
select : 0.530656
|
||||||
|
combination: 0.311731
|
||||||
|
|
||||||
|
Number of elements: 64
|
||||||
|
Number of iterations: 65536
|
||||||
|
---- STARTING TEST ----
|
||||||
|
numeric for: 0.194642
|
||||||
|
ipairs{...}: 0.310673
|
||||||
|
select : 0.909296
|
||||||
|
combination: 0.280604
|
||||||
|
|
||||||
|
Number of elements: 128
|
||||||
|
Number of iterations: 32768
|
||||||
|
---- STARTING TEST ----
|
||||||
|
numeric for: 0.14598
|
||||||
|
ipairs{...}: 0.276733
|
||||||
|
select : 1.36814
|
||||||
|
combination: 0.260261
|
||||||
|
|
||||||
|
Number of elements: 256
|
||||||
|
Number of iterations: 16384
|
||||||
|
---- STARTING TEST ----
|
||||||
|
numeric for: 0.141446
|
||||||
|
ipairs{...}: 0.244541
|
||||||
|
select : 2.375652
|
||||||
|
combination: 0.243239
|
||||||
|
|
||||||
|
Lua: luajit
|
||||||
|
|
||||||
|
Number of elements: 2
|
||||||
|
Number of iterations: 2097152
|
||||||
|
---- STARTING TEST ----
|
||||||
|
numeric for: 0.222393
|
||||||
|
ipairs{...}: 0.221411
|
||||||
|
select : 0.121801
|
||||||
|
combination: 0.112205
|
||||||
|
|
||||||
|
Number of elements: 4
|
||||||
|
Number of iterations: 1048576
|
||||||
|
---- STARTING TEST ----
|
||||||
|
numeric for: 0.126469
|
||||||
|
ipairs{...}: 0.122567
|
||||||
|
select : 0.138826
|
||||||
|
combination: 0.134571
|
||||||
|
|
||||||
|
Number of elements: 8
|
||||||
|
Number of iterations: 524288
|
||||||
|
---- STARTING TEST ----
|
||||||
|
numeric for: 0.076119
|
||||||
|
ipairs{...}: 0.075873
|
||||||
|
select : 0.181403
|
||||||
|
combination: 0.173958
|
||||||
|
|
||||||
|
Number of elements: 16
|
||||||
|
Number of iterations: 262144
|
||||||
|
---- STARTING TEST ----
|
||||||
|
numeric for: 0.056428
|
||||||
|
ipairs{...}: 0.056107
|
||||||
|
select : 0.274034
|
||||||
|
combination: 0.066305
|
||||||
|
|
||||||
|
Number of elements: 32
|
||||||
|
Number of iterations: 131072
|
||||||
|
---- STARTING TEST ----
|
||||||
|
numeric for: 0.041979
|
||||||
|
ipairs{...}: 0.043606
|
||||||
|
select : 0.478121
|
||||||
|
combination: 0.051035
|
||||||
|
|
||||||
|
Number of elements: 64
|
||||||
|
Number of iterations: 65536
|
||||||
|
---- STARTING TEST ----
|
||||||
|
numeric for: 0.033367
|
||||||
|
ipairs{...}: 0.035739
|
||||||
|
select : 0.808869
|
||||||
|
combination: 0.040126
|
||||||
|
|
||||||
|
Number of elements: 128
|
||||||
|
Number of iterations: 32768
|
||||||
|
---- STARTING TEST ----
|
||||||
|
numeric for: 0.025935
|
||||||
|
ipairs{...}: 0.029247
|
||||||
|
select : 1.473057
|
||||||
|
combination: 0.034267
|
||||||
|
|
||||||
|
Number of elements: 256
|
||||||
|
Number of iterations: 16384
|
||||||
|
---- STARTING TEST ----
|
||||||
|
numeric for: 0.023097
|
||||||
|
ipairs{...}: 0.026557
|
||||||
|
select : 2.668443
|
||||||
|
combination: 0.030818000000001
|
||||||
|
|
68
vararg-iteration/run.lua
Normal file
68
vararg-iteration/run.lua
Normal file
|
@ -0,0 +1,68 @@
|
||||||
|
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
|
Loading…
Add table
Reference in a new issue