Re: modified reverse

new topic     » goto parent     » topic index » view thread      » older message » newer message

rforno writes:
> The following modified reverse routine is simpler and, 
> most of the times, slightly faster than the standard one. 
> However, I noticed that the comparison between the two 
> is affected by the type of data (integer or not,
> simple or compound sequence), and even it seems that 
> the way of generating the data (by means of a single 
> sentence or by a loop) affects the resulting
> timings. Any explanation for such a behavior?
>
> global function reverse1(sequence s)
>    integer n
>    sequence t
>    n = length(s)
>    t = repeat(0,n)
>    for i = 1 to n do
>         t[n] = s[i]
>         n -= 1
>    end for
>    return t
> end function

I did a quick test, and your reverse1() above does seem to
be slightly faster than reverse() in misc.e. 
This is counter-intuitive since you do n decrements of n
where the standard reverse() only does n/2 increments.
The number of copies, t[n] = s[i], is the same, since the
standard reverse() does two copies per loop but only
half as many loops.

Timings can be unpredictable for a number of reasons.
Euphoria is doing things, especially with storage allocation,
that are not visible in the code. Also the Pentium chips
depend on caching for speed, and it's hard to predict
whether a given algorithm will cache well. It may be faster
(as in this case) to have a short loop that executes 
many times, rather than a long loop that executes fewer times.
It makes it easier for the Pentium to keep all the 
necessary data and code in its on-chip cache.

I'll try a few more tests and maybe replace the
standard reverse() with yours, or something like it.

Thanks,
   Rob Craig
   Rapid Deployment Software
   http://www.RapidEuphoria.com

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu