unofficial mirror of guile-devel@gnu.org 
 help / color / mirror / Atom feed
From: Andy Wingo <wingo@pobox.com>
To: guile-devel <guile-devel@gnu.org>
Subject: what's a cons worth?
Date: Thu, 14 Oct 2010 20:24:05 +0200	[thread overview]
Message-ID: <m3wrpkab0a.fsf@unquote.localdomain> (raw)

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/



                 reply	other threads:[~2010-10-14 18:24 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/guile/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=m3wrpkab0a.fsf@unquote.localdomain \
    --to=wingo@pobox.com \
    --cc=guile-devel@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).