[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: Deleting all elements from a table
- From: Luis Carvalho <carvalho@...>
- Date: Wed, 13 Sep 2006 23:16:38 -0400
> >>local k= next(t)
> >>while k ~= nil do
> >> t[k]= nil
> >> k= next(t)
> >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
Applied Math PhD Student - Brown University
PGP Key: E820854A <firstname.lastname@example.org>