[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: list iteration for statement
- From: "John Belmonte" <jvb@...>
- Date: Wed, 13 Jun 2001 10:26:37 +0900
ET wrote:
> In C? C always gets the raw arguments. And a vararg Lua function
> may still get the 'n' field. It's not redundant. Try
>
> print(getn{nil,0,nil})
I see your point there!
A few messages back you corrected my statement about the vm not knowing
about lists, but I should have said the vm doesn't know how to compute n
given an arbitrary table. That is what had to be added when I implemented
the list-for loop. As you say the standard library's implementation of getn
is arbitrary, and it may not be a good idea to contaminate the vm with it.
I'm not looking to tackle the list semantics of Lua, I think it's a can of
worms. With the list-for, I was just trying to make an convenient and fast
equivalent of the foreachi function, which covers at least a third of the
loops I write.
I did a simple timing comparison between the following two loops, in the
case where "n" is already defined, and sum is a local:
for i = 1, getn(t) do
sum = sum + t[i]
end
for v in t do
sum = sum + v
end
The list-for is about 20% faster when t is local, and 30% faster when t is
global.
-John