[Haskell-cafe] Can Haskell outperform C++?

Ertugrul Söylemez es at ertes.de
Sun May 6 13:40:59 CEST 2012

"Janek S." <fremenzone at poczta.onet.pl> wrote:

> a couple of times I've encountered a statement that Haskell programs
> can have performance comparable to programs in C/C++. I've even read
> that thanks to functional nature of Haskell, compiler can reason and
> make guarantess about the code and use that knowledge to automatically
> parallelize the program without any explicit parallelizing commands in
> the code. I haven't seen any sort of evidence that would support such
> claims. Can anyone provide a code in Haskell that performs better in
> terms of execution speed than a well-written C/C++ program? Both
> Haskell and C programs should implement the same algorithm (merge sort
> in Haskell outperforming bubble sort in C doesn't count), though I
> guess that using Haskell-specific idioms and optimizations is of
> course allowed.

While Haskell usually gets close to C/C++ speed for number crunching
algorithms it seldomly beats them.  Of course the execution model gives
potential to do so, but currently GHC doesn't do enough low level
optimization.  It is amazing at high level optimization so you often get
similar speeds for much less code that is close to the problem domain.
Combined with the fact that it's more difficult to write incorrect
programs in Haskell there is an overall save in time to get to the
result and that is amplified whenever you have to change the program

Where Haskell really gets beyond C and C++ is in concurrency.
Multithreaded network servers written in Haskell are not only a lot
easier to write, but they also perform better than well written server
programs in C/C++.  This is because Haskell's concurrency goes well with
its parallel execution model, where in machine code you don't really
have a notion of procedures.  You have thunks and they can not only be
executed in parallel, but they can also be moved between threads freely.

Imagine that in C/C++ you would move a client to another thread in the
middle of a function.  The Haskell run-time system does this all the
time to distribute computing time evenly between all CPUs.  Of course
this preemptive execution model can be replicated in C/C++, but that is
an amazingly difficult task, so difficult that you would practically
write very verbose Haskell in C/C++.


nightmare = unsafePerformIO (getWrongWife >>= sex)
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 836 bytes
Desc: not available
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20120506/5c949205/attachment.pgp>

More information about the Haskell-Cafe mailing list