<div dir="ltr">@Niklas<div><br></div><div>You can use the `foldM` form the FoldL package to achieve equal results as the fastest loops in your current benchmark. foldM will allow you to deal with your loops at a high level of abstraction though. See the following post, by Gabriel Gonzalez, for an example:<br>

<a href="http://www.haskellforall.com/2013/08/foldl-100-composable-streaming-and.html">http://www.haskellforall.com/2013/08/foldl-100-composable-streaming-and.html</a></div><div><br></div><div>I have added the bench mark to my fork of your repo, and made a pull request:</div>

<div><a href="https://github.com/Davorak/loop">https://github.com/Davorak/loop</a></div><div><br></div><div>It looks like the only reason that `foldM` does not preform well with `Word32` is because of the naive implementation of `enumFromTo` for Word32 as explained in my other email in more detail.</div>

<div><br></div><div>Here is the Criterion report:</div><div><a href="http://htmlpreview.github.io/?https://github.com/Davorak/loop/blob/master/results/bench.html">http://htmlpreview.github.io/?https://github.com/Davorak/loop/blob/master/results/bench.html</a><br>

</div><div><br></div></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Tue, Apr 29, 2014 at 11:31 AM, Niklas Hambüchen <span dir="ltr"><<a href="mailto:mail@nh2.me" target="_blank">mail@nh2.me</a>></span> wrote:<br>

<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">This is just a short notice that using<br>
<br>
   foldl' (+) 0 [0..100000::Int]<br>
<br>
is over 10 times slower than using<br>
<br>
  flip execState 0 $<br>
    forLoop (0 :: Int) (< n) (+1) $ \i -> do<br>
      x <- get<br>
      put $! x + i<br>
<br>
with `loopFor` as on <a href="https://github.com/nh2/loop" target="_blank">https://github.com/nh2/loop</a>.<br>
<br>
Even using an IORef is twice as fast as the pure foldl' (but still 5<br>
times slower than strict State).<br>
<br>
The benchmark is at<br>
<a href="http://htmlpreview.github.io/?https://github.com/nh2/loop/blob/master/results/bench-foldl-and-iorefs-are-slow.html" target="_blank">http://htmlpreview.github.io/?https://github.com/nh2/loop/blob/master/results/bench-foldl-and-iorefs-are-slow.html</a>.<br>


<br>
(All benchmarks are linked from <a href="https://github.com/nh2/loop" target="_blank">https://github.com/nh2/loop</a>.)<br>
<br>
You can see there that the problem is gone when using Vector.foldl', but<br>
only for Int - for Word32 it persists.<br>
<br>
It seems that manual looping is beneficial even when dealing with prime<br>
examples of pure code that GHC ought to optimize well.<br>
<br>
Niklas<br>
_______________________________________________<br>
Haskell-Cafe mailing list<br>
<a href="mailto:Haskell-Cafe@haskell.org">Haskell-Cafe@haskell.org</a><br>
<a href="http://www.haskell.org/mailman/listinfo/haskell-cafe" target="_blank">http://www.haskell.org/mailman/listinfo/haskell-cafe</a><br>
</blockquote></div><br><br clear="all"><div><br></div>-- <br>Patrick Wheeler<br><a href="mailto:Patrick.John.Wheeler@gmail.com">Patrick.John.Wheeler@gmail.com</a><br><a href="mailto:Patrick.J.Wheeler@rice.edu">Patrick.J.Wheeler@rice.edu</a><br>

<a href="mailto:Patrick.Wheeler@colorado.edu">Patrick.Wheeler@colorado.edu</a>
</div>