[racket] performance of iteration through a vector

From: Dmitry Pavlov (dpavlov at ipa.nw.ru)
Date: Sat Aug 30 18:54:03 EDT 2014

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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20140831/1ec8390a/attachment.html>

Posted on the users mailing list.