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

PUC_Lua Graph

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