[Haskell-cafe] Haskell performance question

Xiao-Yong Jin xj2106 at columbia.edu
Thu Nov 8 14:41:11 EST 2007


Bulat Ziganshin <bulat.ziganshin at gmail.com> writes:

> Hello Dan,
>
> Thursday, November 8, 2007, 9:33:12 PM, you wrote:
>
>> main = do
>>    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) }
>>    x <- readArray a (n-1)
>>    print x
>
> 1. ghc doesn't implement loop unrolling, even in -O2 mode
> 2. forM_ may have its own overheads, i'm not sure
> 3. add strictness annotations:
>
> forM_ [0..n-2] $ \i -> do { return $! i;
>                             x <- readArray a i; return $! x;
>                             y <- readArray a (i+1); return $! y;
>                             writeArray a (i+1) (x+y) }
>
> such cycle should be approx. 3-10 times slower than C one

I didn't know you can do that.  Anyway, I tried OP's C and
Haskell versions and the one with your strictness
annotations.  It seems that the `$!' thing doesn't do much,
or it's not doing anything.


OP's C code:

$ time ./test
100000000.000000

real    0m1.128s
user    0m0.572s
sys     0m0.555s


OP's Haskell code with n=1e8:

$ time ./test-ghc 
1.0e8

real    0m2.065s
user    0m1.505s
sys     0m0.558s


OP's Haskell code with n=1e8 and your strictness annotations:

$ time ./test-ghc 
1.0e8

real    0m2.064s
user    0m1.472s
sys     0m0.591s


However, bad performance observed by OP is not observed by me.

$ ghc --version
The Glorious Glasgow Haskell Compilation System, version 6.8.1
$ gcc --version
gcc (GCC) 4.1.2 (Gentoo 4.1.2 p1.0.2)
Copyright (C) 2006 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

-- 
    c/*    __o/*
    <\     * (__
    */\      <


More information about the Haskell-Cafe mailing list