You can get better performance out of the recursive function by using
car/cdr instead of first/rest; first/rest require their arguments to
be lists, whereas car/cdr require theirs to be pairs, which is a lot
cheaper to check. Also, using an optional argument (in a loop,
especially) makes a difference.

Even so, for/sum wins, because it uses ops like unsafe-car and
unsafe-cdr, which it can do safely, since it first establishes that
it's working with a list (and then doesn't have to check again).

- Jon

On Thu, Jul 27, 2017 at 8:55 PM, Daniel Prager
<daniel.a.pra...@gmail.com> wrote:
> Revised to collect garbage before each timing shows the recursive function
> isn't too bad (but not great):
>
> cpu time: 405 real time: 404 gc time: 0
> 2452578471
>
> cpu time: 368 real time: 368 gc time: 0
> 2452578471
>
> cpu time: 50 real time: 50 gc time: 0
> 2452578471
>
> cpu time: 194 real time: 195 gc time: 75
> 2452578471
>
>
> Dan
>
> --
> You received this message because you are subscribed to the Google Groups
> "Racket Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to racket-users+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to