Skip to content

Instantly share code, notes, and snippets.

@catwell
Created May 2, 2011 11:26
Show Gist options
  • Save catwell/951469 to your computer and use it in GitHub Desktop.
Save catwell/951469 to your computer and use it in GitHub Desktop.
Lua global lookups overhead
-- do global lookups matter?
require "os"
f = function() return end
run = function(n)
start = os.clock()
for i=1,n do f() end
stop = os.clock()
return (stop - start)
end
run2 = function(n)
local g = f
start = os.clock()
for i=1,n do g() end
stop = os.clock()
return (stop - start)
end
for i=3,9 do
j = math.pow(10,i)
s1 = run(j)
s2 = run2(j)
d = s1 - s2
print(string.format("%u: %g / %g (%g)",i,s1,s2,d))
end
-- RESULTS:
-- 3: 8.1e-05 / 6.1e-05 (2e-05)
-- 4: 0.000803 / 0.000597 (0.000206)
-- 5: 0.007189 / 0.004677 (0.002512)
-- 6: 0.0621 / 0.047847 (0.014253)
-- 7: 0.61813 / 0.47468 (0.14345)
-- 8: 6.17794 / 4.74395 (1.43399)
-- 9: 61.8233 / 47.4341 (14.3892)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment