[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Vector and List Performance
From: |
Pascal J. Bourguignon |
Subject: |
Re: Vector and List Performance |
Date: |
Tue, 09 Jun 2009 11:30:37 +0200 |
User-agent: |
Gnus/5.101 (Gnus v5.10.10) Emacs/22.2 (gnu/linux) |
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__