[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: Why can't I do for...in on a table?
- From: Wim Couwenberg <w.couwenberg@...>
- Date: Mon, 12 Sep 2005 20:01:34 +0200
> That makes sense. In the first version, some implicit functions are
> being executed, which are faster than explicitly calling pairs().
Chris, the call of pairs is not important in this example. You have
to keep in mind that a for loop like:
for k, v in pairs(t) do ... end
calls pairs *only once*, regardless of the size of the table. It is
the function "next" that gets called (implicitly) on each iteration.
And though this is the same function as it was in Lua 4, in Lua 5.x it
is now executed as any other C function call in a script. This, plus
additional stack slot copying, incurs the overhead.
> Seems like a 2x improvement in this sort of thing is A Good Thing (tm).
That stands. :-) The factor can be at most this high, it will drop
if the loop actually performs something useful...