Created
December 18, 2012 10:06
-
-
Save anonymous/4326807 to your computer and use it in GitHub Desktop.
Micro-benchmark in Lua for comparing two methods of dealing with varargs
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
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