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 <john.hind@zen.co.uk> wrote:

> 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.

Actually, since the length operator is O(log n) it
can sometimes make sense to maintain an index manually
in a local even when it is not natural in the loop.

See the discussion between Luiz and me on this
Stack Overflow answer:

http://stackoverflow.com/questions/19807239/lua-iterate-through-every-possible-k-length-string-given-a-list-of-symbols/19808175#19808175

-- 
Pierre Chapuis