help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Vector and List Performance


From: Tassilo Horn
Subject: Re: Vector and List Performance
Date: Mon, 08 Jun 2009 21:57:29 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/23.0.94 (gnu/linux)

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

Hi!

>   (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? What have I missed?

I think that there's some optimization under the hood.  Actually, you
always access the same place in the list/vector.  With real random
access, you get the expected result:

--8<---------------cut here---------------start------------->8---
(defun bench ()
  (let* ((len 1000000)
         (vec (make-vector len 0))
         (lst (make-list   len 0))
         (times 50000))
    (cons
     (/ (car (benchmark-run times (aref vec (random len)))) times)
     (/ (car (benchmark-run times (nth (random len) lst)))  times))))

(bench)
==> (1.5999999999999999e-09 . 1.2723999999999999e-06)
--8<---------------cut here---------------end--------------->8---

So random access on a vector is about 1000 times faster.

Bye,
Tassilo





reply via email to

[Prev in Thread] Current Thread [Next in Thread]