Last active
February 26, 2020 18:48
-
-
Save ExtReMLapin/eb7b371964ddd3cddd3ef46b45d05ca5 to your computer and use it in GitHub Desktop.
benchmarking lua elsif vs hashtable
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local nBranch = 1 | |
local nLoops = 2000000 | |
print("NUMBER_BRANCHS,TIME_ELSEIF,TIME_LOOKUP") | |
while (nBranch <= 100) do | |
io.write(nBranch) | |
io.write(",") | |
local valueName = "iInner" | |
local elseifCodeTable = {"local iOutter = 1\nwhile iOutter <= " .. nLoops .. " do\n\tlocal iInner = 1\n\tlocal dummy;\n\twhile iInner <= " .. nBranch .. " do\n", "\t\tend\n\t\tiInner = iInner + 1\n\tend\n\tiOutter = iOutter + 1\nend"} | |
local i = 1 | |
while (i <= nBranch) do | |
if i ~= 1 then | |
table.insert(elseifCodeTable, #elseifCodeTable, string.format("\t\telseif %s == %i then\n\t\t\t dummy = true\n", valueName, i)) | |
else | |
table.insert(elseifCodeTable, #elseifCodeTable, string.format("\t\tif %s == 1 then\n\t\t\t dummy = true\n", valueName)) | |
end | |
i = i + 1 | |
end | |
local elseifFunction, err = loadstring(table.concat(elseifCodeTable)) | |
if not elseifFunction then error(err) end | |
local time = os.clock() | |
elseifFunction() | |
io.write(os.clock() - time) | |
io.write(",") | |
local lookupTable = {} | |
i = 1 | |
while (i <= nBranch) do | |
lookupTable[i] = true | |
i = i + 1 | |
end | |
local function lookUpFunction() | |
local iOutter = 1 | |
while (iOutter <= nLoops) do | |
local iInner = 1 | |
while iInner <= nBranch do | |
local dummy = lookupTable[iInner] | |
iInner = iInner + 1 | |
end | |
iOutter = iOutter + 1 | |
end | |
end | |
time = os.clock() | |
lookUpFunction() | |
io.write(os.clock() - time) | |
io.write("\n") | |
nBranch = nBranch + 1 | |
end |
Author
ExtReMLapin
commented
Dec 2, 2019
•
Here are the results using PUC-lua:
NUMBER_BRANCHS,TIME_ELSEIF,TIME_LOOKUP
1,0.107,0.096
2,0.16,0.14
3,0.272,0.23
4,0.343,0.243
5,0.433,0.319
6,0.617,0.344
7,0.748,0.337
8,0.912,0.404
9,1.123,0.475
10,1.35,0.554
11,1.458,0.561
12,1.819,0.59
13,2.023,0.597
14,2.323,0.702
15,2.546,0.751
16,2.96,0.73
17,3.309,0.837
18,3.606,0.918
19,3.829,0.893
20,4.214,0.884
21,4.734,1.056
22,5.139,1.295
23,5.715,1.052
24,5.769,1.167
25,6.157,1.165
26,6.688,1.21
27,7.048,1.171
28,7.506,1.337
29,8.029,1.454
30,9.1,1.288
31,9.157,1.401
32,9.61,1.411
33,10.272,1.436
34,10.768,1.475
35,11.318,1.621
36,11.948,1.644
37,12.471,1.669
38,13.006,1.656
39,13.518,1.691
40,14.265,1.775
41,14.963,1.846
42,15.878,1.828
43,16.24,1.988
44,17.126,1.876
45,17.732,2.043
46,18.589,2.06
47,19.688,2.277
48,20.391,2.115
49,20.81,2.229
50,21.505,2.263
51,22.774,2.271
52,23.103,2.303
53,24.292,2.285
54,25.066,2.442
55,25.998,2.2950000000001
56,26.769,2.453
57,27.636,2.474
58,28.91,2.545
59,29.99,2.635
60,30.652,2.83
61,31.969,2.748
62,32.769,2.8270000000001
63,33.738,2.7729999999999
64,34.488,2.6959999999999
65,35.821,2.819
66,37.433,3.087
67,37.632,2.9
68,39.145,3.0349999999999
69,40.069,3.1689999999999
70,40.911,3.0469999999998
71,41.686,3.8920000000001
72,44.316,3.248
73,44.544,3.0900000000001
74,45.353,3.1219999999998
75,46.339,3.307
76,47.682,3.191
77,48.361,3.3059999999998
78,50.24,3.336
79,51.759,3.4960000000001
80,52.909,3.4159999999999
81,54.311,3.729
82,56.206,3.6880000000001
83,57.17,3.585
84,58.137,3.855
85,59.874,3.7270000000001
86,61.221,3.8860000000002
87,62.229,3.7040000000002
88,63.234,3.9470000000001
89,65.761,3.9430000000002
90,69.189,4.1620000000003
91,69.851,4.0529999999999
92,69.675,4.636
93,71.732,4.136
94,72.308,4.1330000000003
95,73.739,4.239
96,75.403,4.1599999999999
97,78.109,4.3800000000001
98,78.06,4.1770000000001
99,78.434,4.5119999999997
100,81.095,4.3359999999998
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment