[racket] performance of iteration through a vector

From: Sean Kanaley (skanaley at gmail.com)
Date: Sat Aug 30 19:20:04 EDT 2014

The generic (for ([var seq]) is slow regardless of vector/list/etc. But
there is often a specific "in-" form. On my machine

(for ((j (in-range 1000000)))
    (for ((v (in-vector vec)))
      (set! sum (+ sum v))))

is very slightly faster than the explicit vector ref version, 7.3... vs.
7.6... (32 bit Ubuntu).


On Sat, Aug 30, 2014 at 6:54 PM, Dmitry Pavlov <dpavlov at ipa.nw.ru> wrote:

> Hello,
>
> Consider the following program:
>
> (define n 100)
> (define vec (for/vector ((i (in-range n))) i))
>
> (let ((t (current-inexact-milliseconds))
>       (sum 0))
>   (for ((j (in-range 1000000)))
>     (for ((i (in-range n)))
>       (set! sum (+ sum (vector-ref vec i)))))
>   (displayln (/ (- (current-inexact-milliseconds) t) 1000.0)))
>
> (let ((t (current-inexact-milliseconds))
>       (sum 0))
>   (for ((j (in-range 1000000)))
>     (for ((v vec))
>       (set! sum (+ sum v))))
>   (displayln (/ (- (current-inexact-milliseconds) t) 1000.0)))
>
>
> On my system (64-bit linux, Racket 6.1.0.2), it gives the following result:
>
> 1.016682861328125
> 6.3261611328125
>
>
> So we can make a conclusion that (for ((v vec)) ...) is
> 6x slower than (for ((i (in-range n))) ... (vector-ref vec i) ...)
> Is it normal? Would you advise to use the explicit (vector-ref)
> when performance matters?
>
> Best regards
>
> Dmitry
>
>
> ____________________
>   Racket Users list:
>   http://lists.racket-lang.org/users
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20140830/3f7af542/attachment.html>

Posted on the users mailing list.