(define f
    (lambda (x)
      (if (< x 3)
          x
          (+ (f (- x 1))
             (* 2 (f (- x 2)))
             (* 3 (f (- x 3)))))))

;;--
(define (f-tail n)
    (define f-i 
      (lambda (x y z cnt)
        (if (= cnt 2)
            z
            (f-i y z (+ (* 3 x) (* 2 y) z) (- cnt 1)))))
    (f-i 0 1 2 n))

 

Petite Chez Scheme Version 8.4 中

> (time (f 31))
(time (f 31))
    66 collections
    8396 ms elapsed cpu time, including 9 ms collecting
    8410 ms elapsed real time, including 7 ms collecting
    278813272 bytes allocated, including 286100240 bytes reclaimed
145681761292

 

(time (f-tail 31))
(time (f-tail 31))
    no collections
    0 ms elapsed cpu time
    0 ms elapsed real time
    2048 bytes allocated
145681761292

 ikarus 中明显快许多

> (time (f 31))
running stats for (f 31):
    no collections
    1424 ms elapsed cpu time, including 0 ms collecting
    723 ms elapsed real time, including 0 ms collecting
    2000 bytes allocated
145681761292

 

> (time (f-tail 31))
running stats for (f-tail 31):
    no collections
    0 ms elapsed cpu time, including 0 ms collecting
    0 ms elapsed real time, including 0 ms collecting
    360 bytes allocated
145681761292

 

Posted on 2013-02-20 17:32  ASOCS  阅读(145)  评论(0编辑  收藏  举报