Re: Fast appending and sorting of alot of short strings
On 9 Oct 2004, at 20:19, Pete Lomax wrote:
>
>
> 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:
> }}}
<eucode>
> 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
> </eucode>
{{{
>
> 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.
Perfect example of why global variables are sometimes a necessity for
speed, even if some people claim they are bad programming practice. Ditto
for "goto". Thanks, Pete.
Kat
|
Not Categorized, Please Help
|
|