[racket] Math - factorial.rkt, binomial.rkt and memoization

From: Laurent (laurent.orseau at gmail.com)
Date: Tue Apr 9 08:51:21 EDT 2013

On Tue, Apr 9, 2013 at 2:42 PM, Matthew Flatt <mflatt at cs.utah.edu> wrote:

>  (for/product ([p (in-range (+ m 1) (+ n 1))]) p)

This fact/for variant is a clear winner:
> (time (void (fact/for 1000000)))
cpu time: 6948 real time: 6956 gc time: 964
> (time (void (factorial 1000000)))
cpu time: 9936 real time: 9951 gc time: 3700
> (time (void (fact 1000000)))
cpu time: 8445 real time: 8460 gc time: 2273

But I don't fully understand why a simple (for/product ([p (in-range 1
(add1 n))]) p) is as fast as the iota variants.
I see that the latter makes many more small products, which are certainly
faster, but it also does more products of 2 big numbers, whereas
for/product makes only big*small products. Is that a sufficient reason?

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20130409/26d51fcf/attachment.html>

Posted on the users mailing list.