unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
From: pjb@informatimago.com (Pascal J. Bourguignon)
To: help-gnu-emacs@gnu.org
Subject: Re: Vector and List Performance
Date: Tue, 09 Jun 2009 11:30:37 +0200	[thread overview]
Message-ID: <7c63f5n48y.fsf@pbourguignon.anevia.com> (raw)
In-Reply-To: c32bcfbc-f5a1-4bc4-a2fd-075d8f6e9558@e20g2000vbc.googlegroups.com

Nordlöw <per.nordlow@gmail.com> writes:

> I'm trying to figure the performance different between lists and
> vectors. Here is my mockup:
>
>   (defun bench (&rest forms)
>     "Convenience wrapper for benchmark-run-compiled."
>     (/ (nth 0 (benchmark-run 1024 forms)) 1024))
>
> (let ((length 1000000))
>   (cons
>    (bench (aref (make-vector length 0) (/ length 2)))
>    (bench (nth (/ length 2) (make-list length 0)))
>    ))
>
> Strangely I can't seem to find any significant different in
> performance when accessing the middle element in a long vector and
> long list. Shouldn't the random access performance be the big
> difference between vectors and lists? 

Tyere is.


> What have I missed?

You have missed that functions receive their argument already evaluated.
The argument to each of the calls to bench is 0, in both case.

(require 'cl) ; as always
(loop 
    with length = 1000000
    with index  = (1- length)
    for seq in (list (make-vector length 0) (make-list length 0))
    do (insert (format "%8S %s" (type-of seq)
                       (time (loop repeat 100 do (elt seq index))))))

C-x C-e inserts:

  vector Time: 2.030000e+02 ms
    cons Time: 7.819890e+05 ms



Note however that adding or removing elements from the head, or even
in the middle of a list is much faster than doing the same to a
vector.


-- 
__Pascal Bourguignon__


  parent reply	other threads:[~2009-06-09  9:30 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-06-08 17:27 Vector and List Performance Nordlöw
2009-06-08 19:57 ` Tassilo Horn
2009-06-09  9:30 ` Pascal J. Bourguignon [this message]
2009-06-09 11:30   ` Nordlöw
2009-06-09 12:51     ` Pascal J. Bourguignon
2009-06-09 18:13       ` Nordlöw
2009-06-10  8:24         ` Pascal J. Bourguignon
     [not found]         ` <7c63f45we1.fsf@pbourguignon.informatimago.com>
2009-06-10  8:36           ` Pascal J. Bourguignon

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/emacs/

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

  git send-email \
    --in-reply-to=7c63f5n48y.fsf@pbourguignon.anevia.com \
    --to=pjb@informatimago.com \
    --cc=help-gnu-emacs@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).