lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


local function append(x, arg1, ...)
	if arg1 == nil then
		return x
	else
		return arg1, append(x, ...)
	end
end

local tinsert = table.insert
local function append_t(x, ...)
	local t = {...}
	tinsert(t, x)
	return unpack(t)
end

do
	t = os.clock()
	for i = 1, 100000 do
		append_t(1,1,1,1,1)
	end
	print(os.clock() - t)
end

do
	t = os.clock()
	for i = 1, 100000 do
		append(1,1,1,1,1)
	end
	print(os.clock() - t)
end



I wrote one using recursive, and have a little test on it.
Indeed, append() is faster than appent_t() if there are not many(for
my machine, less than 10) elements in ...

2008/11/11 Javier Guerra Giraldez <javier@guerrag.com>:
> On Tuesday 11 November 2008, Shmuel Zeigerman wrote:
>> There are two known tricks that will work:
>> a) packing ... into a table (and storing the 'n' value), and
>> b) using recursive function
>
> like this:
>
> function app (extra, a, ...)
>        if select('#', ...) > 1 then
>                return a, app (extra, ...)
>        else
>                return a, ..., extra
>        end
> end
>
> but i'm not positive that it's faster than using a table
>
> --
> Javier
>