<div dir="ltr">All,<div><br></div><div>Thanks for the pointer to (in-vector). It indeed works faster than</div><div>the explicit (vector-ref) or even (unsafe-vector-ref).</div><div><br></div><div>And yes, I should get around to Typed Racket someday.</div>
<div><br></div><div>Regards,</div><div><br></div><div>Dmitry</div><div><br></div><div><br></div></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Sun, Aug 31, 2014 at 9:19 AM, Robby Findler <span dir="ltr"><<a href="mailto:robby@eecs.northwestern.edu" target="_blank">robby@eecs.northwestern.edu</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Or, use typed racket.<br>
<span class="HOEnZb"><font color="#888888"><br>
Robby<br>
</font></span><div class="HOEnZb"><div class="h5"><br>
On Sat, Aug 30, 2014 at 8:18 PM, Matthew Butterick <<a href="mailto:mb@mbtype.com">mb@mbtype.com</a>> wrote:<br>
> Usually you'll get the best performance from `for` loops by using the in-*<br>
> iterators.<br>
> In this case, you can use `in-vector` [1], which on my machine is faster:<br>
><br>
> (let ((t (current-inexact-milliseconds))<br>
> (sum 0))<br>
> (for ((j (in-range 1000000)))<br>
> (for ((v (in-vector vec)))<br>
> (set! sum (+ sum v))))<br>
> (displayln (/ (- (current-inexact-milliseconds) t) 1000.0)))<br>
><br>
> I assume this is a synthetic example, but you can also improve performance<br>
> by using appropriate versions of `for`. This is faster than the last one:<br>
><br>
> (let ((t (current-inexact-milliseconds)))<br>
> (for*/fold ([sum 0])([j (in-range 1000000)][v (in-vector vec)])<br>
> (+ sum v))<br>
> (displayln (/ (- (current-inexact-milliseconds) t) 1000.0)))<br>
><br>
> And this is faster still:<br>
><br>
> (let ((t (current-inexact-milliseconds)))<br>
> (for*/sum ([j (in-range 1000000)][v (in-vector vec)])<br>
> v)<br>
> (displayln (/ (- (current-inexact-milliseconds) t) 1000.0)))<br>
><br>
><br>
> [1]<br>
> <a href="http://docs.racket-lang.org/reference/sequences.html?q=in-vector#%28def._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._in-vector%29%29" target="_blank">http://docs.racket-lang.org/reference/sequences.html?q=in-vector#%28def._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._in-vector%29%29</a><br>
><br>
><br>
> On Aug 30, 2014, at 3:54 PM, Dmitry Pavlov <<a href="mailto:dpavlov@ipa.nw.ru">dpavlov@ipa.nw.ru</a>> wrote:<br>
><br>
> Hello,<br>
><br>
> Consider the following program:<br>
><br>
> (define n 100)<br>
> (define vec (for/vector ((i (in-range n))) i))<br>
><br>
> (let ((t (current-inexact-milliseconds))<br>
> (sum 0))<br>
> (for ((j (in-range 1000000)))<br>
> (for ((i (in-range n)))<br>
> (set! sum (+ sum (vector-ref vec i)))))<br>
> (displayln (/ (- (current-inexact-milliseconds) t) 1000.0)))<br>
><br>
> (let ((t (current-inexact-milliseconds))<br>
> (sum 0))<br>
> (for ((j (in-range 1000000)))<br>
> (for ((v vec))<br>
> (set! sum (+ sum v))))<br>
> (displayln (/ (- (current-inexact-milliseconds) t) 1000.0)))<br>
><br>
><br>
> On my system (64-bit linux, Racket 6.1.0.2), it gives the following result:<br>
><br>
> 1.016682861328125<br>
> 6.3261611328125<br>
><br>
><br>
> So we can make a conclusion that (for ((v vec)) ...) is<br>
> 6x slower than (for ((i (in-range n))) ... (vector-ref vec i) ...)<br>
> Is it normal? Would you advise to use the explicit (vector-ref)<br>
> when performance matters?<br>
><br>
> Best regards<br>
><br>
> Dmitry<br>
><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>
><br>
><br>
><br>
> On Aug 30, 2014, at 3:54 PM, Dmitry Pavlov <<a href="mailto:dpavlov@ipa.nw.ru">dpavlov@ipa.nw.ru</a>> wrote:<br>
><br>
> Hello,<br>
><br>
> Consider the following program:<br>
><br>
> (define n 100)<br>
> (define vec (for/vector ((i (in-range n))) i))<br>
><br>
> (let ((t (current-inexact-milliseconds))<br>
> (sum 0))<br>
> (for ((j (in-range 1000000)))<br>
> (for ((i (in-range n)))<br>
> (set! sum (+ sum (vector-ref vec i)))))<br>
> (displayln (/ (- (current-inexact-milliseconds) t) 1000.0)))<br>
><br>
> (let ((t (current-inexact-milliseconds))<br>
> (sum 0))<br>
> (for ((j (in-range 1000000)))<br>
> (for ((v vec))<br>
> (set! sum (+ sum v))))<br>
> (displayln (/ (- (current-inexact-milliseconds) t) 1000.0)))<br>
><br>
><br>
> On my system (64-bit linux, Racket 6.1.0.2), it gives the following result:<br>
><br>
> 1.016682861328125<br>
> 6.3261611328125<br>
><br>
><br>
> So we can make a conclusion that (for ((v vec)) ...) is<br>
> 6x slower than (for ((i (in-range n))) ... (vector-ref vec i) ...)<br>
> Is it normal? Would you advise to use the explicit (vector-ref)<br>
> when performance matters?<br>
><br>
> Best regards<br>
><br>
> Dmitry<br>
><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>
><br>
><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>
____________________<br>
Racket Users list:<br>
<a href="http://lists.racket-lang.org/users" target="_blank">http://lists.racket-lang.org/users</a><br>
<br>
</div></div></blockquote></div><br></div>