Skip to content

Instantly share code, notes, and snippets.

Created December 18, 2012 10:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/4326807 to your computer and use it in GitHub Desktop.
Save anonymous/4326807 to your computer and use it in GitHub Desktop.
Micro-benchmark in Lua for comparing two methods of dealing with varargs
require 'pl'
local select = select
function sum_select (...)
local sum = 0
local n = select('#',...)
for i = 1,n do
sum = sum + select(i,...)
end
return sum
end
function sum_arr (...)
local sum = 0
local n = select('#',...)
local args = {...}
for i = 1,n do
sum = sum + args[i]
end
return sum
end
collectgarbage 'stop'
local start_gc, msg_gc
function gc_start (msg)
if start_gc then
gc_finish()
end
start_gc = collectgarbage 'count'
msg_gc = msg
end
function gc_finish ()
print(('%s: mem %7.1f kb'):format(msg_gc,collectgarbage 'count' - start_gc))
start_gc = 0
end
gc_start 'select'
test.timer('select',1e6,sum_select,1,2,3,4,5,6)
gc_start 'arr'
test.timer('arr',1e6,sum_arr,1,2,3,4,5,6)
gc_finish()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment