lua-users home
lua-l archive

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


> I'm a curious guy so I decided to test out your theory.  In my simple 
> timing tests I couldn't see a significant performance difference between 
> ipairs and a list generator I implemented (below).

That depends on how you do the testing. You can try a lot of loops
(where each one creates its own closure), with each loop very short (so
it does not dilute that cost) ;-). Something like

  local t = {1,2}
  for i=1,2^20 do
    local s=0
    for k in list(t) do s=s+k end
  end

(I think something like that appeared in the list some time ago.)


> The passing of var_1 to the generator function in the for loop seems
> arbitrary.  Indeed, I didn't need it in the list implementation, and
> could likewise picture situations where more than var_1 are
> needed.  Why not just rely on closures?

It is arbitrary, but it covers the two most typical cases (pairs &
ipairs). Of course closures give a more general solution, but for those
typical cases <<var_1 + "table">> do the job and avoids garbage creation.

-- Roberto