Skip to content

Instantly share code, notes, and snippets.

@ExtReMLapin
Last active February 26, 2020 18:48
Show Gist options
  • Save ExtReMLapin/eb7b371964ddd3cddd3ef46b45d05ca5 to your computer and use it in GitHub Desktop.
Save ExtReMLapin/eb7b371964ddd3cddd3ef46b45d05ca5 to your computer and use it in GitHub Desktop.
benchmarking lua elsif vs hashtable
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
@ExtReMLapin
Copy link
Author

ExtReMLapin commented Dec 2, 2019

NUMBER_BRANCHS,TIME_ELSEIF,TIME_LOOKUP
1,0,0
2,0.015,0.031
3,0,0.022
4,0.031,0.016
5,0.031,0.038
6,0.031,0.054
7,0.046,0.054
8,0.047,0.053
9,0.078,0.054
10,0.084,0.063
11,0.084,0.069
12,0.116,0.085
13,0.116,0.084
14,0.147,0.085
15,0.138,0.1
16,0.163,0.101
17,0.185,0.115
18,0.216,0.111
19,0.223,0.121
20,0.235,0.123
21,0.263,0.123
22,0.302,0.182
23,0.36,0.162
24,0.401,0.154
25,0.402,0.172
26,0.427,0.191
27,0.432,0.221
28,0.496,0.183
29,0.527,0.171
30,0.566,0.21
31,0.598,0.253
32,0.606,0.193
33,0.656,0.192
34,0.683,0.222
35,0.763,0.245
36,0.835,0.264
37,1.022,0.257
38,0.934,0.267
39,0.921,0.233
40,1.015,0.325
41,1.054,0.241
42,0.968,0.253
43,1.102,0.295
44,1.305,0.301
45,1.288,0.302
46,1.382,0.337
47,1.393,0.326
48,1.578,0.389
49,1.526,0.397
50,1.597,0.316
51,1.432,0.338
52,1.538,0.322
53,1.558,0.339
54,1.701,0.358
55,1.756,0.331
56,1.75,0.374
57,1.745,0.357
58,1.876,0.413
59,2.015,0.395
60,2.072,0.39700000000001
61,2.077,0.4
62,2.095,0.464
63,2.425,0.447
64,2.315,0.464
65,2.539,0.44800000000001
66,2.737,0.595
67,2.794,0.51900000000001
68,2.858,0.485
69,2.895,0.518
70,2.914,0.50700000000001
71,2.904,0.488
72,2.935,0.491
73,3.096,0.47999999999999
74,3.206,0.52200000000001
75,2.925,0.48699999999999
76,3.025,0.48
77,3.033,0.49900000000001
78,3.256,0.50699999999999
79,3.333,0.53999999999999
80,3.312,0.50999999999999
81,3.39,0.518
82,3.421,0.52100000000002
83,3.521,0.63900000000001
84,4.11,0.60900000000001
85,3.682,0.55000000000001
86,3.938,0.57999999999998
87,3.891,0.55199999999999
88,3.892,0.56
89,4.02,0.56100000000001
90,4.093,0.57299999999998
91,4.247,0.29500000000002
92,4.348,0.30000000000001
93,4.402,0.30100000000002
94,4.52,0.30199999999999
95,4.52,0.30500000000001
96,4.612,0.33600000000001
97,4.826,0.34700000000001
98,5.526,0.33499999999998
99,4.917,0.34799999999998
100,5.143,0.376

@SirLynix
Copy link

SirLynix 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

@ExtReMLapin
Copy link
Author

PUC_Lua Graph

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment