[Haskell-cafe] benchmarking c/c++ and haskell

Andrew Coppin andrewcoppin at btinternet.com
Sun Sep 12 07:32:42 EDT 2010

Vo Minh Thu wrote:
> Hi,
> I would like to benchmark C/C++ and Haskell code. The goal is to
> improve the Haskell port[0] of smallpt[1].
> To make sure my approach was reliable, I got the code of two programs
> (one in C, the other in Haskell) from a post[2] by Don. The code is
> reproduced below. When timing the execution of both program, I have a
>> 4x difference. It is said on the blog the programs should have
> similar performance.
> I simply don't get the reason of such a difference. I've tried the
> code on my Atom netbook and also on an older centrino machine. The
> timing are similar (i.e. the C and Haskell program show >4x
> difference). Both machines have GHC 6.12.1 on Linux.
> Would you have an idea?

The function floor :: Double -> Int is surprisingly slow under GHC. 
(IIRC, it's implemented by converting Double -> (Int, Integer) -> Int, 
or something equally absurd.) Poking around GHC.Prim directly allows you 
to do the same operation much, much faster. I couldn't say exactly how 
much of a difference it makes, but I've had programs go from seconds to 
microseconds just by switching this.


My solution was to do this:


Uh... good luck! o_O

(Of course, I could be speaking complete nonesense and this bug has 
since been fixed, or the cause of the slowness in smallpt is something 

More information about the Haskell-Cafe mailing list