• Subject: Re: explicitly defining table length + _ARG proposal
• From: Geoff Leyland <geoff_leyland@...>
• Date: Wed, 23 Jun 2010 08:52:57 +1200

```On 23/06/2010, at 7:53 AM, Patrick Donnelly wrote:

> ... I think most everyone agrees select() is a
> performance detriment...

I think most everyone hasn't even heard of Lua :-)

There's probably a tradeoff between select and extra heap allocations (which I think most everyone agrees should be kept to a minimum!).  According to this flawed benchmark of an irrelevant use case on my machine, select is better up to about 6 arguments in Lua and about 3 in luajit-2.0.0-beta4 (or head, I can't remember).  The memory use figures aren't that useful.  Improvements welcome.

Cheers,
Geoff

\$ lua vararg_test.lua
repeats	args	stack(s)	heap(s)  	stack(kB)	heap(kB)
...
10000	1	0.003299*	0.006973 	33.559570	64.191406
10000	2	0.005001*	0.007792 	33.559570	51.488281
10000	3	0.005865*	0.008507 	33.559570	69.472656
10000	4	0.007374*	0.009208 	33.559570	76.863281
10000	5	0.008789*	0.009900 	33.559570	72.300781
10000	6	0.010912 	0.010664*	33.559570	66.613281
10000	7	0.011312*	0.011323 	33.559570	75.253906
10000	8	0.012905 	0.012138*	33.559570	71.738281
10000	9	0.014974 	0.012679*	33.559570	56.847656
10000	10	0.016616 	0.013335*	33.559570	57.332031
...

local repeats
local sum

local function f1(...)
for i = 1, select('#', ...) do
sum = sum + select(i, ...)
end
end

local function f2(...)
local t = {...}
for i = 1, #t do
sum = sum + t[i]
end
end

local function test(f, ...)
local c = os.clock()
sum = 0
for i = 1, repeats do
f(...)
end
local m = collectgarbage("count")
collectgarbage("collect")
return os.clock() - c, m
end

local function report(t1, m1, t2, m2, ...)
io.write(("%d\t%d\t%f%s\t%f%s\t%f\t%f\n"):
format(repeats, select('#',...),
t1, t1 < t2 and "*" or " ", t2, t2 < t1 and "*" or " ", m1, m2))
end

local function test2(...)
local t1, m1 = test(f1, ...)
local t2, m2 = test(f2, ...)
report(t1, m1, t2, m2, ...)
end

local function test3(...)
for i = select('#', ...), 1, -1 do
test2(select(i, ...))
end
end

repeats = 1000
io.write("repeats\targs\tstack(s)\theap(s)  \tstack(kB)\theap(kB)\n")
repeat
test3(3, 5, 7, 11, 13, 17, 19, 23, 29, 31)
repeats = repeats * 10
until repeats > 1000000

```