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

John Lato jwlato at gmail.com
Wed Nov 4 21:27:29 EST 2009


I forgot to add that changing your definition of Particle and adding
some optimization tunings yields an appreciable speedup.

> data Particle = Particle { pos, vel :: {-# UNPACK #-} !Vector3,
>                            mass, rho, prs :: {-# UNPACK #-} !Scalar }


john$ ghc --make -O2 -fexcess-precision -fforce-recomp sph.hs
-funfolding-creation-threshold=600 -funfolding-use-threshold=200
john$ time ./sph 300

real	0m14.963s
user	0m14.307s
sys	0m0.265s

and

john$ ghc --make -O2 -fexcess-precision -fvia-c -optc=-O3
-fforce-recomp sph.hs -funfolding-creation-threshold=600
-funfolding-use-threshold=200

john$ time ./sph 300
real	0m13.393s
user	0m12.771s
sys	0m0.247s

which is quite close to the C++ performance.  Using ByteStrings
instead of Strings for output gets it the rest of the way.

If you want to put on your Dons hat, the next steps would be using
something like Acovea to fine-tune the compiler options, and/or using
DPH.

Best,
John


On Thu, Nov 5, 2009 at 1:49 AM, John Lato <jwlato at gmail.com> wrote:
> 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.
>
> and
>
> 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.
>
> Cheers,
> John
>
>>
>> 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