[Haskell-beginners] Performance of Idiomatic lazy Haskell

Daniel Fischer daniel.is.fischer at web.de
Mon Feb 1 11:10:46 EST 2010


Am Montag 01 Februar 2010 15:59:59 schrieb Markus Böhm:
> 1. I used Your lualoop file with content:
> module Main (main) where
>
> main :: IO ()
> main = do
>    --putStrLn "EPS:"
>    -- eps <- readLn :: IO Double
>    print $ 4*calcPi 0.00000001
>
> calcPi :: Double -> Double
> calcPi eps = go False 1 3
>      where
>        go bl p1 i
>
>            | 4*abs(p2-p1) < eps    = p1
>            | otherwise             = go (not bl) p2 (i+2)
>
>              where
>                p2  | bl        = p1+1/i
>
>                    | otherwise = p1-1/i
>

Oops, I've screwed up my timings earlier, that variant doesn't quite give 
optimal speed (3.65s via-C vs.3.02s for the other loops, but the NCG code 
of that takes a whopping 12.13s here vs. 4.x - 6.y s for the other loops), 
what I had measured was

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

(which gives 3.03s via C and 6.91s with the NCG). Mind trying that, too?

Nevertheless, the results are seriously disturbing.

The previous code runs more than 2.5 times as fast on your computer than on 
mine when compiled with the NCG and twice as fast on my computer than on 
yours when compiled via C.

I don't know what to make of it.

> 2.
> F:\MeineUebungen>ghc -O2 -fforce-recomp -fexcess-precision -fvia-C
> -optc-O3 -o c loop --make p085-pi_lualoop.hs
> [1 of 1] Compiling Main             ( p085-pi_lualoop.hs,
> p085-pi_lualoop.o ) Linking cloop.exe ...
>
>
> F:\MeineUebungen>ghc -O2 -fforce-recomp -o nloop --make
> p085-pi_lualoop.hs [1 of 1] Compiling Main             (
> p085-pi_lualoop.hs, p085-pi_lualoop.o ) Linking nloop.exe ...
>
> F:\MeineUebungen>cloop +RTS -sstderr
> cloop +RTS -sstderr
> 3.1415926485894725
>           20,860 bytes allocated in the heap
>              892 bytes copied during GC
>            3,068 bytes maximum residency (1 sample(s))
>           13,316 bytes maximum slop
>                1 MB total memory in use (0 MB lost due to fragmentation)
>
>   Generation 0:     0 collections,     0 parallel,  0.00s,  0.00s
> elapsed Generation 1:     1 collections,     0 parallel,  0.00s,  0.00s
> elapsed
>
>   INIT  time    0.02s  (  0.02s elapsed)
>   MUT   time    7.39s  (  7.27s elapsed)
>   GC    time    0.00s  (  0.00s elapsed)
>   EXIT  time    0.00s  (  0.00s elapsed)
>   Total time    7.41s  (  7.28s elapsed)
>
>   %GC time       0.0%  (0.0% elapsed)
>
>   Alloc rate    2,816 bytes per MUT second
>
>   Productivity  99.8% of total user, 101.5% of total elapsed
>
>
> F:\MeineUebungen>nloop +RTS -sstderr
> nloop +RTS -sstderr
> 3.1415926485894725
>           20,860 bytes allocated in the heap
>              892 bytes copied during GC
>            3,068 bytes maximum residency (1 sample(s))
>           13,316 bytes maximum slop
>                1 MB total memory in use (0 MB lost due to fragmentation)
>
>   Generation 0:     0 collections,     0 parallel,  0.00s,  0.00s
> elapsed Generation 1:     1 collections,     0 parallel,  0.00s,  0.00s
> elapsed
>
>   INIT  time    0.02s  (  0.00s elapsed)
>   MUT   time    4.77s  (  4.81s elapsed)
>   GC    time    0.00s  (  0.00s elapsed)
>   EXIT  time    0.00s  (  0.00s elapsed)
>   Total time    4.78s  (  4.81s elapsed)
>
>   %GC time       0.0%  (0.0% elapsed)
>
>   Alloc rate    4,362 bytes per MUT second
>
>   Productivity  99.7% of total user, 99.0% of total elapsed
>
>
> -- Markus


More information about the Beginners mailing list