lua-users home
lua-l archive

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

On Wed, Nov 27, 2013 at 1:28 PM, John Hind <> wrote:
>  and there is no reason I can see why the standard libraries should not be, at least partly, implemented in Lua.

And this can be just as efficient.  There is a distinct overhead to
calling into C - for instance, using table.insert to append to a

I know the saying goes that there are lies, damned lies and then
microbenchmarks, but here are some numbers:

(this also includes example C implementations of and table.append)

The test involves creating a table and appending N items, and doing
this M times.

A) table.insert
B) t[#t+1]=...
C) t[#t+1] as function
D) t[i]=... in loop
E) table.append; two values at a time

                    A   B   C   D   E
M=10000,N=1000,{} : 1.5  1.2  1.3  0.6  1.1
M=10000,N=1000,new: 1.4  1.1  1.6  0.5  0.9
M=1000,N=10000,{} : 1.7  1.4  1.4  0.6  1.2
M=1000,N=10000,new: 1.6  1.3  1.6  0.4  1.0

As has been pointed out, table.insert is slower than t[#t+1], in fact
it's usually slower than t[#t+1] done in a Lua function, which gives
you an idea of the cost involved with that C call.  (This is good
news, because t[#t+1] may be better but it really is ugly and hard to
type).  But best of all is just setting t[i] in a loop - nearly 3
times as fast.

(I'm using local aliases for table.insert etc - just plain
table.insert would give us 2.1 instead of 1.6.)

I was testing table.append, which appends multiple values to a table,
and it's no better than table.insert at appending one element, however
it does get more efficient if you are appending more than one item at
a time.  This is a case which is harder to do efficiently in Lua with
varargs because we either have to create a table or use the (slow)
select function.

The 'new' refers to using, which is just a wrapper around
lua_createtable.  It can make a difference, depends on your case, but
it's not a radical difference.

So use numeric for and avoid calling C functions if you want to do
sequences as fast as possible.  I'm not seeing any clear case here for
adding new and append to table, however.

Now this doesn't mean 'ipairs Considered Harmful' because it _can_ be
convenient. It's just not the right construct for inner loops.

steve d.