[racket] Tail recursive module cons

From: Laurent (laurent.orseau at gmail.com)
Date: Sat Mar 29 07:19:54 EDT 2014

Some comments:
1) Never run benchmarks inside DrRacket (I suspect this is what you did
because the reported times are high)
2) Why do you include the garbage collections in the time measurment?
That's probably not what you want to measure (plus the gc should before the
3) The need to call `reverse` in `list-incr/acc` is sufficient alone to
blow the recorded times (looks like it's what you were saying).

Here is I think a more appropriate benchmark, summing the values in a list:

#lang racket
(define (list-sum/direct lst)
    [(empty? lst) 0]
    [else (+ (car lst)
             (list-sum/direct (cdr lst)))]))

(define (list-sum/acc lst (acc 0))
    [(empty? lst) acc]
    [else (list-sum/acc (cdr lst)
                        (+ (car lst) acc))]))

(define huge-list
  (build-list 50000 identity))

 (for ([i 2000])
   (list-sum/direct huge-list)))

; cpu time: 6440 real time: 6481 gc time: 100

 (for ([i 2000])
   (list-sum/acc huge-list)))

; cpu time: 1508 real time: 1537 gc time: 8

Here the tail recursion version is 4x faster.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20140329/3c04ce28/attachment-0001.html>

Posted on the users mailing list.