[Haskell-cafe] Haskell performance question
Don Stewart
dons at galois.com
Thu Nov 8 14:53:28 EST 2007
dpiponi:
> I see lots of shootout examples where Haskell programs seem to perform
> comparably with C programs, but I find it hard to reproduce anything
> like those figures when testing with my own code. So here's a simple
> case:
>
> I have this C program:
>
> #include <stdio.h>
>
> #define n 100000000
>
> double a[n];
>
> int main()
> {
> int i;
> for (i = 0; i<n; ++i)
> {
> a[i] = 1.0f;
> }
> for (i = 0; i<n-1; ++i)
> {
> a[i+1] = a[i]+a[i+1];
> }
> printf("%f\n",a[n-1]);
> }
>
> And this Haskell program:
>
> import Data.Array.IO
> import Control.Monad
>
> n = 10000000
>
> 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
Be really careful with Doubles (i.e. check what Int arrays also do),
since you need special flags to get good performance from unboxed Double
arrays. The best effort I know of is:
http://shootout.alioth.debian.org/gp4/benchmark.php?test=spectralnorm&lang=all
which took some time to work out the right path through the compiler. In
essence:
* some gcc flags:
-optc-O3 -optc-march=pentium4 -optc-mfpmath=sse -optc-msse2 -optc-ffast-math
* Foreign.Marshal.Array
* type Reals = Ptr Double
* inlinePerformIO
in the end I'm stunned at how well we did there, since Double math has
been a long term issue.
Perhaps there are other lessons we can extract from that code?
-- Don
More information about the Haskell-Cafe
mailing list