[racket] streams, recursion and running out of memory

From: Rodolfo Carvalho (rhcarvalho at gmail.com)
Date: Wed Feb 22 11:40:08 EST 2012

Hello Joe,


2012/2/22 Joe Gilray <jgilray at gmail.com>

> Hi Matthew,
>
> Thanks for your help.  I really can't even pretend to understand the
> make-coroutine code but this is what I'm seeing:
>
> Original make-coroutine, without stream-ref pre-call => out of memory
> (1GB) when running (prime-pi 500000)
>


I tried to run your code here and I also get out of memory in the first
case.




> Original make-coroutine, with stream-ref pre-call => runs (prime-pi
> 500000) in about .4 sec
>


Just as a reference to the other results, here it gives: cpu time: 328 real
time: 328 gc time: 94.




> Alternate make-coroutine, with or without stream-ref pre-call => runs
> (prime-pi 500000) in about 28 sec
>
>
With stream-ref pre-call: cpu time: 343 real time: 344 gc time: 78
Without: cpu time: 19609 real time: 19719 gc time: 9172




I changed `prime-pi' into `prime-pi/acc' as below and then I got the best
results:

(define (prime-pi/acc n)
  (let loop-count ([candidate-stream primes]
                   [sum 0])
    (if (> (stream-first candidate-stream) n)
        sum
        (loop-count (stream-rest candidate-stream) (add1 sum)))))


With original make-coroutine: cpu time: 234 real time: 235 gc time: 0
With Matthew's make-coroutine: cpu time: 187 real time: 187 gc time: 0



All measurements made from within DrRacket using (time (prime-pi 500000))
or (time (prime-pi/acc 500000)).

[]'s

Rodolfo
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20120222/b54a82dc/attachment.html>

Posted on the users mailing list.