[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Lua 5.1 (alpha): table.insert()?
- From: Mike Pall <mikelu-0509@...>
- Date: Sun, 4 Sep 2005 14:47:53 +0200
Hi,
PA wrote:
> In Lua 5.1, is there any advantages/differences in using table.insert()
> vs using the new length operator directly?
Why don't you try? I never cease to be amazed why questions
get asked that can be resolved with one-liners:
First a test for a single huge table: [lower numbers are better]
$ TIMEFORMAT="%U"
$ time lua -e 'local t={}; for i=1,1e6 do table.insert(t, 1) end'
0.901
$ time lua -e 'local t,f={},table.insert; for i=1,1e6 do f(t, 1) end'
0.709
$ time lua -e 'local t={}; for i=1,1e6 do t[#t+1] = 1 end'
0.579
Then a somewhat more realistic test with many smaller tables:
$ time lua -e 'for k=1,1e4 do local t={}; for i=1,100 do table.insert(t, 1) \
end end'
0.719
$ time lua -e 'local f=table.insert; for k=1,1e4 do local t={}; \
for i=1,100 do f(t, 1) end end'
0.551
$ time lua -e 'for k=1,1e4 do local t={}; for i=1,100 do t[#t+1] = 1 end end'
0.427
And for comparison the same tests when you know you can use
a local (!) counter (or resolve #t just once):
$ time lua -e 'local t={}; for i=1,1e6 do t[i] = 1 end'
0.194
$ time lua -e 'local t,x={},1; for i=1,1e6 do t[x] = 1; x=x+1 end'
0.222
$ time lua -e 'for k=1,1e4 do local t={}; for i=1,100 do t[i] = 1 end end'
0.256
$ time lua -e 'for k=1,1e4 do local t,x={},1; for i=1,100 do \
t[x] = 1; x=x+1 end end'
0.289
YMMV of course.
Bye,
Mike