<div dir="ltr">The generic (for ([var seq]) is slow regardless of vector/list/etc. But there is often a specific "in-" form. On my machine<div><br></div><div><div>(for ((j (in-range 1000000)))</div><div> (for ((v (in-vector vec)))</div>
<div> (set! sum (+ sum v))))</div></div><div><br></div><div>is very slightly faster than the explicit vector ref version, 7.3... vs. 7.6... (32 bit Ubuntu).</div></div><div class="gmail_extra"><br><br><div class="gmail_quote">
On Sat, Aug 30, 2014 at 6:54 PM, Dmitry Pavlov <span dir="ltr"><<a href="mailto:dpavlov@ipa.nw.ru" target="_blank">dpavlov@ipa.nw.ru</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<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><span class="HOEnZb"><font color="#888888"><div><br></div><div>Dmitry</div><div><br></div></font></span></div>
<br>____________________<br>
Racket Users list:<br>
<a href="http://lists.racket-lang.org/users" target="_blank">http://lists.racket-lang.org/users</a><br>
<br></blockquote></div><br></div>