Clean the table

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Clean the table

Marcos E. Wurzius
I have a table: 
t={ 1,2,3,4,...9999,10000; a="new", b="old"}

Which is the best and faster way to clean 
the lfieldlist "[1]...[10000]" from the table?

I do:

for i=1,getn(t) do tremove(t,i) end

but it's very slow.

Another way:
for i=1,getn(t) do t[i]=nil end
t.n=0
This is faster.

An idea...


-- 
Marcos

Reply | Threaded
Open this post in threaded view
|

Re: Clean the table

Denis Lamarche
How about copying the entries that you do want into a new table, then make the old table equal to the new one (leaving all that other stuff to the gc). Would that work?

new_t={a="new", b="old}
t=new_t


From: "Marcos E. Wurzius" <[hidden email]>
Reply-To: [hidden email]
To: Multiple recipients of list <[hidden email]>
Subject: Clean the table
Date: Tue, 29 Jan 2002 16:19:51 -0400

I have a table:
t={ 1,2,3,4,...9999,10000; a="new", b="old"}

Which is the best and faster way to clean
the lfieldlist "[1]...[10000]" from the table?

I do:

for i=1,getn(t) do tremove(t,i) end

but it's very slow.

Another way:
for i=1,getn(t) do t[i]=nil end
t.n=0
This is faster.

An idea...


--
Marcos




_________________________________________________________________
Get your FREE download of MSN Explorer at http://explorer.msn.com/intl.asp.


Reply | Threaded
Open this post in threaded view
|

Re: Clean the table

Enrico Colombini
In reply to this post by Marcos E. Wurzius
"Marcos E. Wurzius" <[hidden email]> wrote Tue, 29 Jan 2002:

>I have a table: 
>t={ 1,2,3,4,...9999,10000; a="new", b="old"}
>
>Which is the best and faster way to clean 
>the lfieldlist "[1]...[10000]" from the table?
>
>I do:
>
>for i=1,getn(t) do tremove(t,i) end
>
>but it's very slow.

Apart from the other suggested solutions, you should remove array elements
backwards:

  for i = 1, getn(t) do tremove(t, getn(t)) end

or, better and faster:

  for i = 1, getn(t) do tremove(t) end

The difference in running time *is* significant: adding or removing from
the beginning of an array appears to run in O(n) time, while acting on the
end of the array seems to run in constant time (disclaimer: just my
hurriedly-run tests). 
So your loop takes O(n^2) time if run forwards, O(n) time if run backwards;
on my machine, for 10000 elements I get approx. 12 seconds against 0.05
seconds.

  Enrico