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/