chicken-users
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Chicken-users] Good way to code the equivalent to this?


From: Elf
Subject: Re: [Chicken-users] Good way to code the equivalent to this?
Date: Mon, 25 Aug 2008 06:22:34 -0700 (PDT)

On Mon, 25 Aug 2008, Tobia Conforto wrote:

Elf wrote:
(define a
  (alist->hash-table
    (let loop ((i 0))
      (if (fx= 250000 i)
          '()
          (cons (cons (random 500000)
                      (random 500000))
                (loop (fx+ 1 i)))))
    =))


for an improvement in time (surprisingly), use

(define a
  (alist->hash-table
    (let loop ((i 0)
               (r '()))
      (if (fx= 250000 i)
          r
          (loop (fx+ 1 i)
                (cons (cons (random 500000)
                            (random 500000))
                      r))))
    =))

...there are a lot more minor GCs this way

Why is it surprising that the tail-recursive version performs better? And why do you say it has more GC? Isn't it supposed to produce less garbage?


well, i say it has more cause thats what the stats said :) the reason why it would be more in large cases like this is because it has to repeatedly
copy a large structure in the iterative-tail-recursion version, whereas
in the stacking recursion it just has to copy the frames onto the heap every so often.

-elf





reply via email to

[Prev in Thread] Current Thread [Next in Thread]