This is just a short notice that using
foldl' (+) 0 [0..100000::Int]
is over 10 times slower than using
flip execState 0 $
forLoop (0 :: Int) (< n) (+1) $ \i -> do
x <- get
put $! x + i
with `loopFor` as on https://github.com/nh2/loop.
Even using an IORef is twice as fast as the pure foldl' (but still 5
times slower than strict State).
The benchmark is at
http://htmlpreview.github.io/?https://github.com/nh2/loop/blob/master/results/bench-foldl-and-iorefs-are-slow.html.
(All benchmarks are linked from https://github.com/nh2/loop.)
You can see there that the problem is gone when using Vector.foldl', but
only for Int - for Word32 it persists.
It seems that manual looping is beneficial even when dealing with prime
examples of pure code that GHC ought to optimize well.
Niklas
_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe