Send Beginners mailing list submissions to
        [email protected]

To subscribe or unsubscribe via the World Wide Web, visit
        http://www.haskell.org/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
        [email protected]

You can reach the person managing the list at
        [email protected]

When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."


Today's Topics:

   1. Re:  Performance of Idiomatic lazy Haskell (Stephen Tetley)
   2.  one more step backwards (John Moore)
   3. Re:  Performance of Idiomatic lazy Haskell (Daniel Fischer)
   4. Re:  Performance of Idiomatic lazy Haskell (Stephen Tetley)
   5. Re:  Performance of Idiomatic lazy Haskell (Daniel Fischer)
   6. Re:  one more step backwards (Stephen Tetley)
   7. Re:  Performance of Idiomatic lazy Haskell (Stephen Tetley)


----------------------------------------------------------------------

Message: 1
Date: Sun, 31 Jan 2010 15:10:33 +0000
From: Stephen Tetley <[email protected]>
Subject: Re: [Haskell-beginners] Performance of Idiomatic lazy Haskell
Cc: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1

Arghh, an error in that post that I didn't notice, I send a revision soon...

Apologies Stephen

>
> ---------------------------------------------------
> Leibniz3 (monoidal unfoldr)
> ---------------------------------------------------
>
> $ ./Leibniz3 +RTS -sstderr -RTS
> d:\coding\haskell\cafe\Leibniz3.exe +RTS -sstderr
> EPS:
> Stack space overflow: current size 8388608 bytes.


------------------------------

Message: 2
Date: Sun, 31 Jan 2010 16:05:51 +0000
From: John Moore <[email protected]>
Subject: [Haskell-beginners] one more step backwards
To: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"

Hi ,
     With help I got the following to print out each step in the evaluation
of the add. However I was just wondering if it was possible to make the
evaluation go back one step at a time. I tried to use a get count function
but this doesnt seem so easy.

data Expression = Val Integer
                | Add Expression Expression
                | Subtract Expression Expression
                | Multiply Expression Expression
                | Divide Expression Expression
         deriving Show
demo1 = (Add(Add(Add(Add(Val 6)(Val 5))(Val 10))(Val 7))(Val 30))
evalStep :: Expression ->  Expression
evalStep (Val x)=  (Val x)
evalStep (Add x y)
   = case x of
       (Val a) -> case y of
                    (Val b) -> Val (a+b)
                    left -> Add x (evalStep y)
       right -> Add (evalStep x)y

evaluate :: Expression ->  IO ()
-- Base case
evaluate (Val a) = return ()

-- Recursive case
evaluate e = do
      putStrLn "Evaluating one more step"
      let e' =  (evalStep e)
      putStrLn ("Result is "++(show e'))
      putStrLn "Do another step (y/n)? :"
      c <- getLine
             if (c=="y")then
         evaluate e'
       else putStrLn("Ok you said" ++ show[c] ++ "so that's it" ++ show
getCount)

getCount:: Expression ->  Int
getCount e' = n, n=1

 if(getCount == 1) then
            putStrLn ("Cannot go back")
  else putStrLn ("One step back" ++ show n-1)

All thoughts welcome!

john
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
http://www.haskell.org/pipermail/beginners/attachments/20100131/bdde027c/attachment-0001.html

------------------------------

Message: 3
Date: Sun, 31 Jan 2010 19:10:59 +0100
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] Performance of Idiomatic lazy Haskell
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain;  charset="iso-8859-1"

Am Sonntag 31 Januar 2010 15:52:41 schrieb Stephen Tetley:
> Hi Daniel
>
> Thanks - the figures are very impressive for the stream fusion
> library. I knew the paper, but I hadn't looked at it the
> implementation.
>

Just for the record, the loops (my variant) coded in C take 2.7 seconds 
(icc) resp. 3.0 seconds (gcc) with -O3.
Markus' loop takes 5.32s (icc) resp. 6.04s (gcc) with -O3.
Well, that's largely due to the fact that that takes two divisions per 
iteration (and divisions are very slow on my box).
Rewriting it so that only one division per iteration is necessary:

================================================
module Main (main) where

main :: IO ()
main = do putStrLn "EPS: "
          eps <- readLn :: IO Double
          let pi14 = pisum 1 3 False (eps*0.25)
          putStrLn $ "PI mit EPS "++(show eps)++" = "++ show(4*pi14)

pisum s i b eps
    | d < eps   = s
    | b         = let h = s+d in h `seq` pisum h (i+2) False eps
    | otherwise = let h = s-d in h `seq` pisum h (i+2) True eps
      where
        d = 1/i
================================================

it takes 5.42 seconds (eps = 1e-8) when compiled natively, that loop in C 
is practically indistinguishable from my loop variant.


So stream-fusion or hand-coded loops in Haskell are clearly slower than C 
loops, but don't stink when compiled with the native code generator.

However, I get identical to gcc performance with
ghc -O2 -fexcess-precision -fvia-C -optc-O3 
[-optc-ffast-math doesn't make any difference then] for the hand-coded 
loops (all, my variant, Markus' original, Markus' rewritten - of course, 
each identical to the corresponding C-loop).

Also for stream-fusion.

Some of the list creating variants also profit from compilation via C, 
others not. However, none come near the NCG loop performance, let alone C 
loop performance.

To sum up:
Although GHC's native code generator does a not too bad job with 
arithmetic-heavy loops, gcc is better (even without optimisations).
But if you compile via C, you can get identical performance - 
for these loops.
In general, things are more complicated, often the native code generator 
produces better code than the via-C route. It's usually worth a test which 
produces the better binary, native or via-C.



------------------------------

Message: 4
Date: Sun, 31 Jan 2010 18:21:10 +0000
From: Stephen Tetley <[email protected]>
Subject: Re: [Haskell-beginners] Performance of Idiomatic lazy Haskell
Cc: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1

Hello Daniel

I can get close with a tail recursive + accumulator monoidal unfoldr
and strict triple for the state (leibniz4).

But it does seem to be loosing the point of the initial exercise "to
be idiomatic", also the answers are divergent too...


Best wishes

Stephen

------
Leibniz4 - monodial unfoldr strict triple
------

$ ./Leibniz4 +RTS -sstderr -RTS
d:\coding\haskell\cafe\Leibniz4.exe +RTS -sstderr
EPS:
PI mit EPS 1.0e-8 = 3.1415926526069526
          24,424 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.03s  (  0.00s elapsed)
  MUT   time    4.34s  (  4.45s elapsed)
  GC    time    0.00s  (  0.00s elapsed)
  EXIT  time    0.00s  (  0.00s elapsed)
  Total time    4.38s  (  4.45s elapsed)

  %GC time       0.0%  (0.0% elapsed)

  Alloc rate    5,582 bytes per MUT second

  Productivity  99.3% of total user, 97.5% of total elapsed

------
Leibniz1 - stream fusion
------

$ ./Leibniz1 +RTS -sstderr -RTS
d:\coding\haskell\cafe\Leibniz1.exe +RTS -sstderr
EPS:
PI mit EPS 1.0e-8 = 3.1415926445727678
          24,404 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.61s  (  4.63s elapsed)
  GC    time    0.00s  (  0.00s elapsed)
  EXIT  time    0.00s  (  0.00s elapsed)
  Total time    4.63s  (  4.63s elapsed)

  %GC time       0.0%  (0.0% elapsed)

  Alloc rate    5,276 bytes per MUT second

  Productivity  99.7% of total user, 99.7% of total elapsed


----------


module Main (main) where

import Data.Monoid


data Maybe2 a st = Nothing2 | Just2 a !st
  deriving (Eq,Show)

dummy_eps :: Double
dummy_eps = 0.00000001

main :: IO ()
main = do
    putStrLn "EPS: "
    eps <- return dummy_eps
    let mx = floor (4/eps)
        !k = (mx+1) `quot` 2
    putStrLn $ "PI mit EPS " ++ (show eps) ++ " = " ++ show (leibniz k)

leibniz :: Integer -> Double
leibniz n = (4 *) $ getSum $ step (fromIntegral n)

data Trip = T3 !Int !Bool !Double

step :: Int -> Sum Double
step times = unfoldrMon phi (T3 0 True 1) where
   phi :: Trip -> Maybe2 (Sum Double) Trip
   phi (T3 i _   _) | i == times  = Nothing2
   phi (T3 i sig d) | sig         = Just2 (Sum (1/d)) (T3 (i+1) False (d+2))
                    | otherwise   = Just2 (Sum (negate (1/d))) (T3
(i+1) True (d+2))



unfoldrMon      :: Monoid a => (b -> Maybe2 a b) -> b -> a
unfoldrMon f b0  = step b0 mempty where
   step b acc = case f b of
              Nothing2       -> acc
              Just2 a new_b  -> step new_b  (a `mappend` acc)


------------------------------

Message: 5
Date: Sun, 31 Jan 2010 19:50:34 +0100
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] Performance of Idiomatic lazy Haskell
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain;  charset="iso-8859-1"

Am Sonntag 31 Januar 2010 19:21:10 schrieb Stephen Tetley:
> Hello Daniel
>
> I can get close with a tail recursive + accumulator monoidal unfoldr
> and strict triple for the state (leibniz4).

Beats stream fusion and direct loops comfortably here (3.55s) when compiled 
with the NCG, very narrowly when compiled via C (2.99s).
Congrats.

>
> But it does seem to be loosing the point of the initial exercise "to
> be idiomatic",

Yes. Returning to that,

a) for tasks where it's appropriate/sufficiently natural, hand-coded loops 
are idiomatic, too.

b) use stream-fusion to get rid of intermediate structures.

> also the answers are divergent too...

In how far?

>
>
> Best wishes
>
> Stephen



------------------------------

Message: 6
Date: Sun, 31 Jan 2010 18:52:33 +0000
From: Stephen Tetley <[email protected]>
Subject: Re: [Haskell-beginners] one more step backwards
To: John Moore <[email protected]>
Cc: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1

Hi John

In this case a simple stack tracking the expression as it is reduced
should work, the code below isn't entirely correct as it seems to need
'r' input twice to do the first roll-back, but it is along the right
lines.

The function 'evaluate' has been split to pass the stack into the
function 'evalWithStack' which does the recursive work.

Best wishes

Stephen


module RollbackEval where



data Expression = Val Integer
                | Add Expression Expression
                | Subtract Expression Expression
                | Multiply Expression Expression
                | Divide Expression Expression
         deriving Show

demo1 = (Add(Add(Add(Add(Val 6)(Val 5))(Val 10))(Val 7))(Val 30))

evalStep :: Expression ->  Expression
evalStep (Val x)=  (Val x)
evalStep (Add x y)
   = case x of
       (Val a) -> case y of
                    (Val b) -> Val (a+b)
                    left -> Add x (evalStep y)
       right -> Add (evalStep x)y

type Stack = [Expression]

evaluate :: Expression -> IO ()
evaluate exp = do
      stk <- evalWithStack exp [exp]
      putStrLn "Stack:"
      mapM_ (putStrLn . show) stk


evalWithStack :: Expression -> Stack -> IO Stack
-- Base case
evalWithStack (Val a) stk = return stk

-- Recursive case
evalWithStack e stk = do
      putStrLn "Evaluating one more step"
      let e' =  (evalStep e)
      putStrLn ("Result is "++(show e'))
      putStrLn "Do another step (y/n) or rollback (r)? :"
      c <- getLine
      case c of
        "y" -> evalWithStack e' (e':stk)
        "r" -> let (a,stk') = stackBack stk in evalWithStack a stk'
        _   -> do { putStrLn ("Ok you said" ++ show[c]
                                            ++ "so that's it"
                                            ++ show (getCount stk))
                  ; return (e':stk)
                  }


stackBack :: Stack -> (Expression,Stack)
stackBack [a]    = (a,[a])
stackBack (a:as) = (a,as)
stackBack []     = error "Whoops empty - should be unreachable"


getCount :: Stack -> Int
getCount stk = length stk

-------------------


------------------------------

Message: 7
Date: Sun, 31 Jan 2010 19:22:56 +0000
From: Stephen Tetley <[email protected]>
Subject: Re: [Haskell-beginners] Performance of Idiomatic lazy Haskell
Cc: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1

On 31 January 2010 18:50, Daniel Fischer <[email protected]> wrote:


>
> Yes. Returning to that,
>
> a) for tasks where it's appropriate/sufficiently natural, hand-coded loops
> are idiomatic, too.
>

Indeed - originally I looked at Markus's code and only saw that an
unfoldr would be a natural implementation for the Leibniz generating
function. I overlooked the episilon used to control the accuracy which
pollutes the unfold and makes it no clearer (too much haste / speed on
my part).



>> also the answers are divergent too...
>
> In how far?

On my machine, they diverged at the eighth decimal place -

Leibniz4 3.1415926526069526
Leibniz1 3.1415926445727678


Best wishes

Stephen


------------------------------

_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners


End of Beginners Digest, Vol 19, Issue 34
*****************************************

Reply via email to