unofficial mirror of guile-devel@gnu.org 
 help / color / mirror / Atom feed
* what's a cons worth?
@ 2010-10-14 18:24 Andy Wingo
  0 siblings, 0 replies; only message in thread
From: Andy Wingo @ 2010-10-14 18:24 UTC (permalink / raw)
  To: guile-devel

Hello all,

I was wondering what the cost of a cons was, and decided to do an experiment:

    scheme@(guile-user)> (define (count-loop n) (let lp ((n n)) (if (> n 0) (lp (1- n)))))
    scheme@(guile-user)> ,time (count-loop 1000000000)
    clock utime stime cutime cstime gctime
    46.02 45.92  0.00   0.00   0.00   0.00
    scheme@(guile-user)> (/ 46.02 1000000000)
    $1 = 4.602e-8
    scheme@(guile-user)> (define (cons-loop n) (let lp ((n n)) (cons n n) (if (> n 0) (lp (1- n)))))
    scheme@(guile-user)> ,time (cons-loop 1000000000)
    clock utime stime cutime cstime gctime
    109.09 108.43  0.45   0.00   0.00   0.00
    scheme@(guile-user)> (- (/ 109.09 1000000000) $1)
    $2 = 6.307e-8

Now, cons-loop should have optimized out that cons, but it didn't; oh
well. The better for my testing. I ran a loop counting down from an
(American) billion. Then I ran a loop doing the same, but consing a pair
before looping. Memory stayed fairly constant, FWIW.

So if I understand this correctly, on my machine, a 2.2 GHz 64-bit intel
laptop, a cons takes 63 nanoseconds (amortized), or about 140 cycles
(amortized).

Then again there is the cost of the local variable refs, which
count-loop doesn't have, but that makes this the upper bound for cons
cost on this machine.

For what it's worth,

Andy
-- 
http://wingolog.org/



^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2010-10-14 18:24 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2010-10-14 18:24 what's a cons worth? Andy Wingo

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).