lua-users home
lua-l archive

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

On Mon, Sep 11, 2006 at 02:09:02PM -0500, Javier Guerra wrote:
> On Monday 11 September 2006 1:03 pm, Tim Gogolin wrote:
> > Right; I understand this point of view.  The problem though is that
> > table.insert(t, v ) has a hash lookup penalty and a function call
> > penalty.
> >
> > local table_insert = table.insert
> >
> > will get rid of the hash lookup, but the function call is somewhat
> > painful inside a tight loop.
> on my machine (2.4Ghz P4, 512KB, kubuntu Dapper, lua5.1):
> time lua -e 'local t={} for i = 1,1e7 do t[#t+1]=1 end'
> real    0m4.730s
> user    0m4.136s
> sys     0m0.480s
> time lua -e 'local t={} local tinsert=table.insert for i = 1,1e7 do tinsert 
> (t,1) end'
> real    0m5.884s
> user    0m5.100s
> sys     0m0.476s
> less than 25% difference... not significant

Not if you do it once. If you have identified the time spent appending
to tables as being significant in your app, cutting that time by 20%
would be a major coup!

Also, it could be more, since the #t+1 takes time, but if [] didn't take
an arg, finding that would be a C pointer dereference internally, maybe
this gives a feel for that, maybe not:

lua5.1 -e 'local t={1,2,3,4,5} for i = 1,1e8 do t[#t+0]=1 end'  13.33s user 0.00s system 96% cpu 13.851 total
lua5.1 -e 'local t={1,2,3,4,5} for i = 1,1e8 do t[5]=1 end'      9.82s user 0.02s system 91% cpu 10.769 total