Created
November 15, 2021 20:07
-
-
Save kilbith/ce1f9b09e090b5bf14dcae3f4e48c831 to your computer and use it in GitHub Desktop.
Lua - local vs global performance
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 NUMBER_OF_CALLS = 20 | |
x = {} | |
for i = 1, 1e4 do | |
x[string.char(math.random(128))] = "foo" | |
end | |
function x.test() | |
local o = {} | |
for i = 1, 1e6 do | |
local t = {foo = function() return i end} | |
table.insert(o, t) | |
end | |
for i = #o, 1, -1 do | |
table.remove(o, i) | |
end | |
end | |
local b = {} | |
for i = 1, NUMBER_OF_CALLS do | |
local start = os.clock() | |
x.test() | |
local res = (os.clock() - start) * 1000 | |
table.insert(b, res) | |
print("GLOBAL", string.format("%.2f ms", res)) | |
end | |
local bb = 0 | |
for _, v in ipairs(b) do | |
bb = bb + v | |
end | |
print() | |
print("Average (GLOBAL)", string.format("%.2f ms", bb / #b)) | |
print("---------------------------------") | |
print() | |
local test = x.test | |
b = {} | |
for i = 1, NUMBER_OF_CALLS do | |
local start = os.clock() | |
test() | |
local res = (os.clock() - start) * 1000 | |
table.insert(b, res) | |
print("LOCAL", string.format("%.2f ms", res)) | |
end | |
bb = 0 | |
for _, v in ipairs(b) do | |
bb = bb + v | |
end | |
print() | |
print("Average (LOCAL)", string.format("%.2f ms", bb / #b)) | |
print("---------------------------------") | |
print() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment