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:

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

Pierre Chapuis