
On 2/10/07, Donald Bruce Stewart
The following C program was described on #haskell
#include
int main() { double x = 1.0/3.0; double y = 3.0; int i = 1; for (; i<=1000000000; i++) { x = x*y/3.0; y = x*9.0; } printf("%f\n", x+y); }
Which was translated to the following Haskell:
{-# OPTIONS -fexcess-precision #-}
import Text.Printf
main = go (1/3) 3 1
go :: Double -> Double -> Int -> IO () go !x !y !i | i == 1000000000 = printf "%f\n" (x+y) | otherwise = go (x*y/3) (x*9) (i+1)
I've compiled the exactly same programs, passing the same parameters to the compiler as you did, but I've got a very different result: $ ghc -O -fexcess-precision -fbang-patterns -optc-O3 -optc-ffast-math\ -optc-mfpmath=sse -optc-msse2 loop.hs -o haskell_loop $ time ./haskell_loop 3.3333333333333335 real 0m14.092s user 0m14.057s sys 0m0.036s $ ghc --version The Glorious Glasgow Haskell Compilation System, version 6.6 While the haskell program took so long, the C program went really faster than the haskell version: $ gcc -O3 -ffast-math -mfpmath=sse -msse2 -std=c99 loop.c -o c_loop $ time ./c_loop 3.333333 real 0m0.001s user 0m0.000s sys 0m0.000s $ gcc --version gcc (GCC) 4.1.2 20061115 (prerelease) (Debian 4.1.1-21) I'm using debian etch (linux), my processor is a pentium 4 3.0ghz, which has sse and sse2 (or at least /proc/cpuinfo says so). As for memory, it probably doesn't matter much in this test, but I have 512mB of ram. In a similar thread that was posted at comp.lang.functional (it was actually regarding ocaml vs C++, you can find it on google groups at http://tinyurl.com/292ps6 ) the same kind of discrepancy occurred. Showing that this kind of benchmarking is usually not very accurate or, at least, that my processor is not well suited for functional languages :P.