lua-users home
lua-l archive

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

> >>local k= next(t)
> >>while k ~= nil do
> >> t[k]= nil
> >> k= next(t)
> >>end
> >
> >It may be worth remembering that the above loop is quadratic in time.
> >
> >-- Roberto
> >
> So is this (my original proposal) better/faster? ...
> for k in pairs (t) do
>   t [k] = nil
> end -- for

Yes. But I think the loop is quadratic because "next" is being called ab
initio in the fourth line. This should be linear

local k = next(t)
while k ~= nil do
  t[k] = nil
  k = next(t, k) -- note the second argument

and similar to the "pairs" version ("pairs" is a closure on "next"), but might
be slightly slower due to the explicit key comparison to nil.


A mathematician is a device for turning coffee into theorems.
        -- P. Erdos 

Luis Carvalho
Applied Math PhD Student - Brown University
PGP Key: E820854A <>