[plt-scheme] Futures performance
On 05/25/2010 08:53 PM, Groshev Dmitry wrote:
> What am I doing wrong? Here is the code and some tests:
>
> (require scheme/future)
>
> (define (fib n)
> (case n
> [(0) 0]
> [(1) 1]
> [else (let ((a (fib (- n 1)))
> (b (fib (- n 2))))
> (+ a b))]))
>
> (define (fibp n)
> (case n
> [(0) 0]
> [(1) 1]
> [else (let ((a (future (lambda () (fib (- n 1)))))
> (b (future (lambda () (fib (- n 2))))))
> (+ (touch a) (touch b)))]))
>
>
>> (time (fib 30))
>>
> cpu time: 546 real time: 539 gc time: 0
> 832040
>
>> (time (fibp 30))
>>
> cpu time: 8986 real time: 7814 gc time: 391
> 832040
>
Is that actually the code you are running? If I take your code verbatim
I get times like these
;; I set n to 40 so you can see a real difference
cpu time: 6100 real time: 6448 gc time: 0
102334155
cpu time: 6132 real time: 3995 gc time: 0
102334155
But if I change the code so that `fibp' makes recursive calls to `fibp'
instead of `fib' then I see `fibp' taking much longer.
;; n = 33
cpu time: 496 real time: 498 gc time: 0
3524578
cpu time: 17429 real time: 18720 gc time: 748
3524578
Which is similar to your results. What sort of machine are you on? I'm
on a dual-core amd 1.8ghz laptop.