[Haskell-beginners] Performance of Idiomatic lazy Haskell

Daniel Fischer daniel.is.fischer at web.de
Mon Feb 1 08:52:41 EST 2010

Am Montag 01 Februar 2010 14:31:40 schrieb Markus Böhm:
> Daniel, with LuaJIT it needs 1.45 sec cpu-time on my machine with
> attached variant.

That's pretty fast.

> I compiled all our Haskell variants with ghc --make -O2.

Well, GHC isn't as good at loop-optimising as gcc is. Depending on the 
loop, the via-C compiled binaries are between 1.4 and 2.3 times faster than 
the NCG compiled.

> I don't know about -fvia-C, have to find out. I hope I didn't
> distract You with my Lua variant.

No sweat.

> In any case thank You very much for
> your advice. Markus.

You're welcome.

Can you try the below with

ghc -O2 -fexcess-precision -fvia-C -optc-O3 -o luaLoop --make Whatever.hs

and run with

echo '0.00000001' | time ./luaLoop

It's a fairly direct translation of the Lua code, and it runs here more or 
less equally fast as (gcc compiled) C-loops.

module Main (main) where

main :: IO ()
main = do
    putStrLn "EPS:"
    eps <- readLn :: IO Double
    print $ 4*calcPi eps

calcPi :: Double -> Double
calcPi eps = go False 1 3
        go bl p1 i
            | 4*abs(p2-p1) < eps    = p1
            | otherwise             = go (not bl) p2 (i+2)
                p2  | bl        = p1+1/i
                    | otherwise = p1-1/i


More information about the Beginners mailing list