Re: Fast appending and sorting of alot of short strings
- Posted by Pete Lomax <petelomax at blueyonder.co.uk> Oct 09, 2004
- 529 views
On Sat, 09 Oct 2004 05:47:34 -0700, Tommy Carlier <guest at RapidEuphoria.com> wrote: >Here's a piece of code from Win4Eu I applied my theory to it and compared your: >}}} <eucode> >global function addToCollection(sequence collection, object element) > -- Add an element to a collection > integer count, growSize > count = collection[length(collection)] + 1 > growSize = count - length(collection) > if growSize > 0 then > if growSize > GROWSIZE then > collection &= repeat(0, growSize + GROWSIZE) > else collection &= BUFFER > end if > end if > collection[count] = element > collection[length(collection)] = count > return collection >end function ></eucode> {{{ with:
sequence collection procedure addTocollection(object element) -- Add an element to a collection integer count, growSize count = collection[length(collection)] + 1 growSize = count - length(collection) if growSize > 0 then if growSize > GROWSIZE then collection &= repeat(0, growSize + GROWSIZE) else collection &= BUFFER end if end if collection[count] = element collection[length(collection)] = count end procedure
They are identical apart from one is a function acting on and returning a parameter, whereas the other is a procedure acting directly on a file-level variable. It may surprise you to find that it is 66 times faster. The testing code I used to arrive at that figure was:
sequence MyTable collection=newCollection() MyTable=newCollection() integer c1,c2 atom t t=time()+1 c1=0 while t>time() do c1+=1 addTocollection(c1) end while t=time()+1 c2=0 while t>time() do c2+=1 MyTable=addToCollection(MyTable,c2) end while ?c1 ?c2 ?c1/c2 abort(0)
Of course it is a pain to have to duplicate code for each table, but for that kind of gain it is well worth it. Regards, Pete