<html><head><meta http-equiv="Content-Type" content="text/html charset=us-ascii"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;"><div><div>Usually you'll get the best performance from `for` loops by using the in-* iterators. </div><div>In this case, you can use `in-vector` [1], which on my machine is faster:</div><div><br></div><div><div>(let ((t (current-inexact-milliseconds))</div><div> (sum 0))</div><div> (for ((j (in-range 1000000)))</div><div> (for ((v (in-vector vec)))</div><div> (set! sum (+ sum v))))</div><div> (displayln (/ (- (current-inexact-milliseconds) t) 1000.0)))</div></div><div><br></div><div>I assume this is a synthetic example, but you can also improve performance by using appropriate versions of `for`. This is faster than the last one:</div><div><br></div><div><div>(let ((t (current-inexact-milliseconds)))</div><div> (for*/fold ([sum 0])([j (in-range 1000000)][v (in-vector vec)])</div><div> (+ sum v))</div><div> (displayln (/ (- (current-inexact-milliseconds) t) 1000.0)))</div></div><div><br></div><div>And this is faster still:</div><div><br></div><div><div>(let ((t (current-inexact-milliseconds)))</div><div> (for*/sum ([j (in-range 1000000)][v (in-vector vec)])</div><div> v)</div><div> (displayln (/ (- (current-inexact-milliseconds) t) 1000.0)))</div></div><div><br></div><div><br></div><div>[1] <a href="http://docs.racket-lang.org/reference/sequences.html?q=in-vector#(def._((lib._racket/private/base..rkt)._in-vector))">http://docs.racket-lang.org/reference/sequences.html?q=in-vector#%28def._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._in-vector%29%29</a></div><br><div><div><br></div><div><div>On Aug 30, 2014, at 3:54 PM, Dmitry Pavlov <<a href="mailto:dpavlov@ipa.nw.ru">dpavlov@ipa.nw.ru</a>> wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite"><div dir="ltr"><div>Hello,</div><div><br></div><div>Consider the following program:</div><div><br></div><div>(define n 100)<br></div><div>(define vec (for/vector ((i (in-range n))) i))</div><div><br></div><div>(let ((t (current-inexact-milliseconds))</div><div> (sum 0))</div><div> (for ((j (in-range 1000000)))</div><div> (for ((i (in-range n)))</div><div> (set! sum (+ sum (vector-ref vec i)))))</div><div> (displayln (/ (- (current-inexact-milliseconds) t) 1000.0)))</div><div> </div><div>(let ((t (current-inexact-milliseconds))</div><div> (sum 0))</div><div> (for ((j (in-range 1000000)))</div><div> (for ((v vec))</div><div> (set! sum (+ sum v))))</div><div> (displayln (/ (- (current-inexact-milliseconds) t) 1000.0)))</div><div><br></div><div><br></div><div>On my system (64-bit linux, Racket 6.1.0.2), it gives the following result:</div><div><br></div><div><div>1.016682861328125</div><div>6.3261611328125</div></div><div><br></div><div><br></div><div>So we can make a conclusion that (for ((v vec)) ...) is</div><div>6x slower than (for ((i (in-range n))) ... (vector-ref vec i) ...)</div><div>Is it normal? Would you advise to use the explicit (vector-ref)</div><div>when performance matters?</div><div><br></div><div>Best regards</div><div><br></div><div>Dmitry</div><div><br></div></div>____________________<br> Racket Users list:<br> <a href="http://lists.racket-lang.org/users">http://lists.racket-lang.org/users</a><br></blockquote></div><div><br></div></div></div><div><br></div><br><div><div>On Aug 30, 2014, at 3:54 PM, Dmitry Pavlov <<a href="mailto:dpavlov@ipa.nw.ru">dpavlov@ipa.nw.ru</a>> wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite"><div dir="ltr"><div>Hello,</div><div><br></div><div>Consider the following program:</div><div><br></div><div>(define n 100)<br></div><div>(define vec (for/vector ((i (in-range n))) i))</div><div><br></div><div>(let ((t (current-inexact-milliseconds))</div>
<div> (sum 0))</div><div> (for ((j (in-range 1000000)))</div><div> (for ((i (in-range n)))</div><div> (set! sum (+ sum (vector-ref vec i)))))</div><div> (displayln (/ (- (current-inexact-milliseconds) t) 1000.0)))</div>
<div> </div><div>(let ((t (current-inexact-milliseconds))</div><div> (sum 0))</div><div> (for ((j (in-range 1000000)))</div><div> (for ((v vec))</div><div> (set! sum (+ sum v))))</div><div> (displayln (/ (- (current-inexact-milliseconds) t) 1000.0)))</div>
<div><br></div><div><br></div><div>On my system (64-bit linux, Racket 6.1.0.2), it gives the following result:</div><div><br></div><div><div>1.016682861328125</div><div>6.3261611328125</div></div><div><br></div><div><br></div>
<div>So we can make a conclusion that (for ((v vec)) ...) is</div><div>6x slower than (for ((i (in-range n))) ... (vector-ref vec i) ...)</div><div>Is it normal? Would you advise to use the explicit (vector-ref)</div><div>
when performance matters?</div><div><br></div><div>Best regards</div><div><br></div><div>Dmitry</div><div><br></div></div>
____________________<br> Racket Users list:<br> <a href="http://lists.racket-lang.org/users">http://lists.racket-lang.org/users</a><br></blockquote></div><br></body></html>