From 33ff43c8598b40b27f6a8188877a66a45ce4f652 Mon Sep 17 00:00:00 2001 From: Lucas Schwiderski Date: Tue, 11 Oct 2022 23:13:55 +0200 Subject: [PATCH] refactor: Use a more representative code style Removing global indexing takes off quite a bit of run time. --- vararg-iteration/results.log | 320 +++++++++++++++++------------------ vararg-iteration/run.lua | 115 +++++++------ 2 files changed, 217 insertions(+), 218 deletions(-) diff --git a/vararg-iteration/results.log b/vararg-iteration/results.log index 773b4ba..ad7e39b 100644 --- a/vararg-iteration/results.log +++ b/vararg-iteration/results.log @@ -9,328 +9,328 @@ 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 +numeric for: 0.371987 +ipairs{...}: 0.611327 +select : 0.241211 +combination: 0.190263 Number of elements: 4 Number of iterations: 1048576 ---- STARTING TEST ---- -numeric for: 0.417153 -ipairs{...}: 0.545133 -select : 0.328147 -combination: 0.310883 +numeric for: 0.266934 +ipairs{...}: 0.491831 +select : 0.28037 +combination: 0.287743 Number of elements: 8 Number of iterations: 524288 ---- STARTING TEST ---- -numeric for: 0.348244 -ipairs{...}: 0.471477 -select : 0.367715 -combination: 0.371746 +numeric for: 0.227304 +ipairs{...}: 0.459805 +select : 0.334971 +combination: 0.33622 Number of elements: 16 Number of iterations: 262144 ---- STARTING TEST ---- -numeric for: 0.302945 -ipairs{...}: 0.428739 -select : 0.443048 -combination: 0.418883 +numeric for: 0.186637 +ipairs{...}: 0.42781 +select : 0.405969 +combination: 0.379274 Number of elements: 32 Number of iterations: 131072 ---- STARTING TEST ---- -numeric for: 0.288698 -ipairs{...}: 0.415577 -select : 0.602379 -combination: 0.39393 +numeric for: 0.166135 +ipairs{...}: 0.380042 +select : 0.567623 +combination: 0.359938 Number of elements: 64 Number of iterations: 65536 ---- STARTING TEST ---- -numeric for: 0.267878 -ipairs{...}: 0.406569 -select : 0.964695 -combination: 0.390686 +numeric for: 0.143388 +ipairs{...}: 0.353311 +select : 0.954676 +combination: 0.368544 Number of elements: 128 Number of iterations: 32768 ---- STARTING TEST ---- -numeric for: 0.254064 -ipairs{...}: 0.384773 -select : 1.625094 -combination: 0.383372 +numeric for: 0.180336 +ipairs{...}: 0.393123 +select : 1.763199 +combination: 0.336671 Number of elements: 256 Number of iterations: 16384 ---- STARTING TEST ---- -numeric for: 0.247988 -ipairs{...}: 0.384708 -select : 2.943422 -combination: 0.381021 +numeric for: 0.126873 +ipairs{...}: 0.318658 +select : 3.104612 +combination: 0.377221 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 +numeric for: 0.398561 +ipairs{...}: 0.761983 +select : 0.487142 +combination: 0.532769 Number of elements: 4 Number of iterations: 1048576 ---- STARTING TEST ---- -numeric for: 0.436931 -ipairs{...}: 0.563225 -select : 0.487017 -combination: 0.519111 +numeric for: 0.27465 +ipairs{...}: 0.534074 +select : 0.449063 +combination: 0.439299 Number of elements: 8 Number of iterations: 524288 ---- STARTING TEST ---- -numeric for: 0.363167 -ipairs{...}: 0.465143 -select : 0.474101 -combination: 0.489039 +numeric for: 0.205838 +ipairs{...}: 0.412852 +select : 0.409167 +combination: 0.419125 Number of elements: 16 Number of iterations: 262144 ---- STARTING TEST ---- -numeric for: 0.324731 -ipairs{...}: 0.414151 -select : 0.530607 -combination: 0.446816 +numeric for: 0.195762 +ipairs{...}: 0.369171 +select : 0.457718 +combination: 0.406086 Number of elements: 32 Number of iterations: 131072 ---- STARTING TEST ---- -numeric for: 0.306231 -ipairs{...}: 0.392634 -select : 0.661512 -combination: 0.408606 +numeric for: 0.166519 +ipairs{...}: 0.348589 +select : 0.621922 +combination: 0.354629 Number of elements: 64 Number of iterations: 65536 ---- STARTING TEST ---- -numeric for: 0.305991 -ipairs{...}: 0.372077 -select : 1.049758 -combination: 0.379606 +numeric for: 0.151066 +ipairs{...}: 0.331067 +select : 1.026244 +combination: 0.355329 Number of elements: 128 Number of iterations: 32768 ---- STARTING TEST ---- -numeric for: 0.287538 -ipairs{...}: 0.372301 -select : 1.77883 -combination: 0.369424 +numeric for: 0.145573 +ipairs{...}: 0.326475 +select : 1.754032 +combination: 0.3286 Number of elements: 256 Number of iterations: 16384 ---- STARTING TEST ---- -numeric for: 0.287302 -ipairs{...}: 0.358031 -select : 3.178964 -combination: 0.363598 +numeric for: 0.139801 +ipairs{...}: 0.307179 +select : 3.221814 +combination: 0.323516 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 +numeric for: 0.351627 +ipairs{...}: 0.601889 +select : 0.405517 +combination: 0.430988 Number of elements: 4 Number of iterations: 1048576 ---- STARTING TEST ---- -numeric for: 0.364446 -ipairs{...}: 0.466707 -select : 0.390046 -combination: 0.423592 +numeric for: 0.248043 +ipairs{...}: 0.444649 +select : 0.363131 +combination: 0.370977 Number of elements: 8 Number of iterations: 524288 ---- STARTING TEST ---- -numeric for: 0.285324 -ipairs{...}: 0.36883 -select : 0.386637 -combination: 0.402977 +numeric for: 0.188097 +ipairs{...}: 0.332006 +select : 0.353665 +combination: 0.352776 Number of elements: 16 Number of iterations: 262144 ---- STARTING TEST ---- -numeric for: 0.247901 -ipairs{...}: 0.323775 -select : 0.438333 -combination: 0.341976 +numeric for: 0.148284 +ipairs{...}: 0.286253 +select : 0.413759 +combination: 0.303672 Number of elements: 32 Number of iterations: 131072 ---- STARTING TEST ---- -numeric for: 0.234862 -ipairs{...}: 0.302702 -select : 0.545853 -combination: 0.317096 +numeric for: 0.134151 +ipairs{...}: 0.262933 +select : 0.519517 +combination: 0.271823 Number of elements: 64 Number of iterations: 65536 ---- STARTING TEST ---- -numeric for: 0.220949 -ipairs{...}: 0.290624 -select : 0.8336 -combination: 0.292184 +numeric for: 0.121241 +ipairs{...}: 0.242814 +select : 0.816218 +combination: 0.251495 Number of elements: 128 Number of iterations: 32768 ---- STARTING TEST ---- -numeric for: 0.213136 -ipairs{...}: 0.28163 -select : 1.308727 -combination: 0.279163 +numeric for: 0.110836 +ipairs{...}: 0.230441 +select : 1.308072 +combination: 0.239512 Number of elements: 256 Number of iterations: 16384 ---- STARTING TEST ---- -numeric for: 0.215232 -ipairs{...}: 0.272502 -select : 2.274558 -combination: 0.273622 +numeric for: 0.122321 +ipairs{...}: 0.229355 +select : 2.319636 +combination: 0.241176 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 +numeric for: 0.327543 +ipairs{...}: 0.520322 +select : 0.372057 +combination: 0.386067 Number of elements: 4 Number of iterations: 1048576 ---- STARTING TEST ---- -numeric for: 0.330613 -ipairs{...}: 0.42877 -select : 0.371128 -combination: 0.427162 +numeric for: 0.193464 +ipairs{...}: 0.382319 +select : 0.315009 +combination: 0.318594 Number of elements: 8 Number of iterations: 524288 ---- STARTING TEST ---- -numeric for: 0.218039 -ipairs{...}: 0.378982 -select : 0.42071 -combination: 0.388871 +numeric for: 0.131633 +ipairs{...}: 0.286164 +select : 0.309684 +combination: 0.314373 Number of elements: 16 Number of iterations: 262144 ---- STARTING TEST ---- -numeric for: 0.207389 -ipairs{...}: 0.314401 -select : 0.466114 -combination: 0.325659 +numeric for: 0.101963 +ipairs{...}: 0.244499 +select : 0.366027 +combination: 0.255374 Number of elements: 32 Number of iterations: 131072 ---- STARTING TEST ---- -numeric for: 0.203002 -ipairs{...}: 0.265324 -select : 0.530656 -combination: 0.311731 +numeric for: 0.085311 +ipairs{...}: 0.215857 +select : 0.507201 +combination: 0.22148 Number of elements: 64 Number of iterations: 65536 ---- STARTING TEST ---- -numeric for: 0.194642 -ipairs{...}: 0.310673 -select : 0.909296 -combination: 0.280604 +numeric for: 0.07508 +ipairs{...}: 0.208738 +select : 0.780712 +combination: 0.213357 Number of elements: 128 Number of iterations: 32768 ---- STARTING TEST ---- -numeric for: 0.14598 -ipairs{...}: 0.276733 -select : 1.36814 -combination: 0.260261 +numeric for: 0.071918 +ipairs{...}: 0.200097 +select : 1.264535 +combination: 0.211788 Number of elements: 256 Number of iterations: 16384 ---- STARTING TEST ---- -numeric for: 0.141446 -ipairs{...}: 0.244541 -select : 2.375652 -combination: 0.243239 +numeric for: 0.078431 +ipairs{...}: 0.204291 +select : 2.307693 +combination: 0.203908 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 +numeric for: 0.213411 +ipairs{...}: 0.234036 +select : 0.145688 +combination: 0.12318 Number of elements: 4 Number of iterations: 1048576 ---- STARTING TEST ---- -numeric for: 0.126469 -ipairs{...}: 0.122567 -select : 0.138826 -combination: 0.134571 +numeric for: 0.144282 +ipairs{...}: 0.135887 +select : 0.127091 +combination: 0.108254 Number of elements: 8 Number of iterations: 524288 ---- STARTING TEST ---- -numeric for: 0.076119 -ipairs{...}: 0.075873 -select : 0.181403 -combination: 0.173958 +numeric for: 0.071371 +ipairs{...}: 0.074621 +select : 0.158596 +combination: 0.153072 Number of elements: 16 Number of iterations: 262144 ---- STARTING TEST ---- -numeric for: 0.056428 -ipairs{...}: 0.056107 -select : 0.274034 -combination: 0.066305 +numeric for: 0.053648 +ipairs{...}: 0.059854 +select : 0.251541 +combination: 0.064755 Number of elements: 32 Number of iterations: 131072 ---- STARTING TEST ---- -numeric for: 0.041979 -ipairs{...}: 0.043606 -select : 0.478121 -combination: 0.051035 +numeric for: 0.039764 +ipairs{...}: 0.04303 +select : 0.452093 +combination: 0.049187 Number of elements: 64 Number of iterations: 65536 ---- STARTING TEST ---- -numeric for: 0.033367 -ipairs{...}: 0.035739 -select : 0.808869 -combination: 0.040126 +numeric for: 0.03186 +ipairs{...}: 0.037593 +select : 0.809323 +combination: 0.042127 Number of elements: 128 Number of iterations: 32768 ---- STARTING TEST ---- -numeric for: 0.025935 -ipairs{...}: 0.029247 -select : 1.473057 -combination: 0.034267 +numeric for: 0.025502 +ipairs{...}: 0.030757 +select : 1.437088 +combination: 0.035736 Number of elements: 256 Number of iterations: 16384 ---- STARTING TEST ---- -numeric for: 0.023097 -ipairs{...}: 0.026557 -select : 2.668443 -combination: 0.030818000000001 +numeric for: 0.022848 +ipairs{...}: 0.027503 +select : 2.710008 +combination: 0.032227000000001 diff --git a/vararg-iteration/run.lua b/vararg-iteration/run.lua index 56281f6..af26de0 100644 --- a/vararg-iteration/run.lua +++ b/vararg-iteration/run.lua @@ -1,68 +1,67 @@ -local unpack = table.unpack or -function(tab) - return table.remove(tab,1), unpack(tab) +local function v51_unpack(t) + return table.remove(t, 1), unpack(t) end -function try(fnc,n,...) - name = name or "" - ts = os.clock() - for i=1,n do - fnc(...) - end - te = os.clock() - print(te-ts) +local function try(fnc, n, ...) + local start = os.clock() + for _ = 1, n do + fnc(...) + end + print(os.clock()-start) 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)) +local unpack = table.unpack or v51_unpack - print("---- STARTING TEST ----") +for i = 1, 8 do + local values = {} + local n = 2^i + print(("Number of elements: %i"):format(n)) + for j =1, n*2 do + values[#values+1] = j + end + n = 2^22 / n + print(("Number of iterations: %i"):format(n)) - io.write "numeric for: " - try(function(...) - args = {...} - for i=1,#args do - a = args[i] - end - end, n, unpack(values)) + print("---- STARTING TEST ----") - io.write "ipairs{...}: " - try(function(...) - args = {...} - for key, value in ipairs(args) do - a = value - end - end, n, unpack(values)) + io.write "numeric for: " + try(function(...) + local args = {...} + for j = 1, #args do + local _ = args[j] + 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 "ipairs{...}: " + try(function(...) + local args = {...} + for _, value in ipairs(args) do + local _ = value + 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() + io.write "select : " + try(function(...) + for j = 1,select("#",...) do + local _ = select(j,...) + end + end, n, unpack(values)) + + ---[[ + io.write "combination: " + try(function(...) + local n_args = select("#",...) + if n_args <= 16 then + for j = 1, n_args do + local _ = select(j,...) + end + else + local args = {...} + for _, value in ipairs(args) do + local _ = value + end + end + end, n, unpack(values)) + --]] + print() end