lua-users home
lua-l archive

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


Tom Wrensch wrote:
> 
> In short his feature is seriously cool.

Calm down ;-)  Sure, it's nice.  But it's not for free - it's
pretty heavy weight.  Because the for-statement does not manage
any state for the iterator one has to create a closure each time.
I.e. your indexiter creates three (short living) GC'ed objects.
And the function call costs too.  Something one should realize
when using this feature.  

Some numbers about the loop overhead:

function iter(t)
  local i=0
  return function() i=i+1 return t[i] end
end

local t={"a","b","c","d"}
for i=1,1000000 do
  --for j=1,getn(t) do local j=t[j] end    -- Lua: 12.0s
  --for j in iter(t) do end                -- Lua: 28.2s (includes ~4900 gc cycles!)
  --for j=1,t:getn() do local j=t[j] end   -- Sol: 10.8s (dispite old vm?!?)
  --for j over t do end                    -- Sol:  4.1s
end

Ciao, ET.

-- 
irc.openprojects.net #lua