[Haskell-cafe] Re: How to optimize the performance of a code in Haskell?

John Lato jwlato at gmail.com
Wed Nov 4 20:49:48 EST 2009

For me, the Haskell version is about 1.5x slower than the C++ version,
19.6 seconds compared to 12.9.  There wasn't an appreciable difference
between -O and -O2.

Which compiler (and version) are you using?

I got:

john$ g++ -O2 -o sph sph.cpp
john$ time ./sph 300

real	0m12.914s
user	0m11.015s
sys	0m0.326s

john$ g++ --version
i686-apple-darwin9-g++-4.0.1 (GCC) 4.0.1 (Apple Inc. build 5490)
Copyright (C) 2005 Free Software Foundation, Inc.


john$ ghc --make -O2 sph.hs
john$ time ./sph 300

real	0m19.606s
user	0m18.933s
sys	0m0.304s

john$ ghc --version
The Glorious Glasgow Haskell Compilation System, version 6.10.4

on a recent model MacBook, OSX 10.5.8.


> Message: 4
> Date: Wed, 04 Nov 2009 17:53:49 +0100
> From: Kalman Noel <noel.kalman at googlemail.com>
> Subject: [Haskell-cafe] Re: How to optimize the performance of a code
>        in      Haskell?
> To: Masayuki Takagi <kamonama2 at gmail.com>
> Cc: Haskell Cafe <haskell-cafe at haskell.org>
> Message-ID: <4AF1B19D.2010403 at googlemail.com>
> Content-Type: text/plain; charset=ISO-8859-15; format=flowed
> (I take it you accidently wrote to fa.haskell, which is just a mirror of -cafe
> and -beginners, so I'm cc-ing the Café with a full quote.)
> Masayuki Takagi:
>> I'm writing fluid simulation programs with SPH(Smoothed particle
>> hydrodynamics) in Haskell and C++. (The purpose that I write in two
>> languages is to make a workflow that first i write in Haskell for
>> rapid prototyping and accuracy then rewrite in C++ for performance.)
>> I've compared them in performance. Then, although I have already done
>> optimization with profiler, the Haskell code is 20 times slower than
>> the C++ code.
>> I think the performance of the Haskell code is so slow that there can
>> be room for optimization. I'm happy if the Haskell code work 3 times
>> slower than the C++ code at worst.
>> How can I make the Haskell code faster?
>> What information should I refer?
>> The codes are here:
>> http://kamonama.sakura.ne.jp/sph/20091101/sph.hs.zip
>> http://kamonama.sakura.ne.jp/sph/20091101/sph.cpp
>> To run the code in Haskell:
>> $ ghc --make -O sph.hs
>> $ ./sph 300
>> (300 is the time step to be conputed)
>> To run the code in C++:
>> $ g++ -O2 -o sph sph.cpp
>> $ ./sph 300
>> (300 is the time step to be conputed)
>> thanks
> I've not looked at the code, but you'll want ghc to do better optimizations
> than -O. -O2 is what you should use in general. Also, number-crunching often
> profits from -fexcess-precision.

More information about the Haskell-Cafe mailing list