[Haskell-cafe] Haskell performance question
Thomas Schilling
nominolo at googlemail.com
Thu Nov 8 13:57:23 EST 2007
On Thu, 2007-11-08 at 10:33 -0800, Dan Piponi wrote:
> 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
>
> Even though 'n' is 10 times bigger in the C program it runs much
> faster than the Haskell program on my MacBook Pro with Haskell 6.6.1.
> I've tried lots of different combinations of flags that I've found in
> various postings to haskell-cafe but to no avail.
>
> What flags do I need to get at least within a factor of 2 or 3 of C?
> Am I using the wrong kind of array? Or is Haskell always going to be
> 15-20 times slower for this kind of numerical work?
I tried both, but it would be really helpful to see your command line
options. I used:
$ gcc -O3 ghc-bench.c -o ghcbC
ghc-bench.c: In function ‘main’:
ghc-bench.c:16: warning: incompatible implicit declaration of built-in
function ‘printf’
$ ghc --make -O2 ghc-bench.hs
and got:
$ time ./ghc-bench
2.0e7
real 0m0.714s
user 0m0.576s
sys 0m0.132s
$ time ./ghcbC
20000000.000000
real 0m0.305s
user 0m0.164s
sys 0m0.132s
This is on a first-gen Macbook running Ubuntu. 1GB RAM. 1.83Ghz Core
Duo
$ ghc --version
The Glorious Glasgow Haskell Compilation System, version 6.8.0.20071019
$ gcc --version
gcc (GCC) 4.1.2 (Ubuntu 4.1.2-0ubuntu4)
Copyright (C) 2006 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is
NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE.
More information about the Haskell-Cafe
mailing list