[Haskell-cafe] Very fast loops. Now!

Ouyang Jian soyj at 163.com
Mon Feb 12 01:30:36 EST 2007


 I think gcc 4.x have much better optimizations than 3.x since SSA added.I donot know if there are very good IR for functional language optimization. Is it hard for STG language to analysis this kind of code?>     go !x !y !i
>         | i == 1000000000 = printf "%f\n" (x+y)
>         | otherwise       = go (x*y/3) (x*9) (i+1)
btw: I always hope to see a C/C++ language compiler writen by haskell.I think it might be much easier to add a new optimization :)
ÔÚ2007-02-11£¬Rafael Almeida <almeidaraf at gmail.com> дµÀ£º
On 2/10/07, Donald Bruce Stewart <dons at cse.unsw.edu.au> wrote: > The following C program was described on #haskell > > #include <stdio.h> > > 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. _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe at haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20070212/d87e8edd/attachment-0001.htm


More information about the Haskell-Cafe mailing list