Frederico,

On Tue, 2007-05-01 at 13:59 +0200, Federico Squartini wrote:
> I was reading an old post where Hal Daume III was analyzing Haskell
> performance for arrays. 
> He proposed a test program which initializes an array, reverse it a
> number of times, and sums the contents.
> 
> So I wrote a c++ reference program, a naive haskell version using
> lists and I also tweaked a little bit with the IOArray version, which
> should be the fastest. Unfortunately there is a  huge performance gap.
> Haskell is slower by a factor of ten, even when using imperative
> style. 

I think the version using lists is a bit unfair, since in C++ you don't
re-allocate the array on the heap and the Haskell version gives you a
very nice high-level abstraction of lists.

With respect to the imperative version, I would suggest

a) letting the program run for longer so you get more reliable timing.
b) use a similar optimisations that we've done for a demo of modifying
images in-situ for our Gtk2Hs library (in
http://darcs.haskell.org/gtk2hs/demo/fastdraw/FastDraw.hs ):

import Data.Array.Base ( unsafeWrite )

doFromTo 0 255 $ \y ->
  doFromTo 0 255 $ \x ->
    -- Here, writeArray was replaced with unsafeWrite. The latter does
    -- not check that the index is within bounds which has a tremendous
    -- effect on performance.
    --  writeArray  pbData (2+x*chan+y*row) blue  -- checked indexing
    unsafeWrite pbData (2+x*chan+y*row) blue  -- unchecked indexing

Here, doFromTo is much faster and using unsafeWrite instead of
writeArray eliminates the array bound check, which is a big win again.
Then again, it is questionable if you really want to do that kind of
low-level programming in Haskell.

Axel.


_______________________________________________
Haskell mailing list
Haskell@haskell.org
http://www.haskell.org/mailman/listinfo/haskell

Reply via email to