[Haskell-cafe] Haskell performance question

Don Stewart dons at galois.com
Thu Nov 8 17:28:41 EST 2007

> Hello Don,
> Thursday, November 8, 2007, 10:53:28 PM, you wrote:
> >>    a <- newArray (0,n-1) 1.0 :: IO (IOUArray Int Double)
> >>    forM_ [0..n-2] $ \i -> do { x <- readArray a i; y <- readArray a
> >> (i+1); writeArray a (i+1) (x+y) }
> oh, i was stupid. obviously, first thing you need to do is to use
> unsafeRead/unsafeWrite operations. i'm wonder how this code is only
> 20x slower than C version, may be you need to use gcc -O3 -funroll-loops
> and gcc3 (because gcc4 becomes slower)

I think you use a different computer to me. Out of the box, amd64, ghc
6.8, with the same n=100000000, Dan's original code runs:

    $ ghc -O2 A.hs  
    $ time ./A_slow
    ./A_slow  1.60s user 0.50s system 95% cpu 2.193 total

and the C program

    $ gcc -O2 A.c -o A_c               
    $ time ./A_c
    ./A_c  1.03s user 0.52s system 96% cpu 1.612 total

without using any special flags.  A 1.3x slowdown such as this is more
typical of low level array programs.

If I awa a > 5x slow down it would be considered a bug, and its been a
/long/ time since I've seen a 20x slowdown.

Note that with ghc 6.6, we get:

    $ time ./A_66
    A_66: out of memory (requested 1048576 bytes)
    ./A_66  4.94s user 0.78s system 99% cpu 5.735 total

So I know which compiler I prefer. 

-- Don

More information about the Haskell-Cafe mailing list