[Haskell-beginners] Cost center annotations - where/let difference

Daniel Fischer daniel.is.fischer at web.de
Thu Nov 18 08:29:55 EST 2010


On Thursday 18 November 2010 11:24:24, Daniel Seidel wrote:
> Hi,
>
> I was doing some heap profiles and ran into the following issue.
>
> Consider the program fib.hs:
>
> module Main where
>
> fib i | i == 0 || i == 1 = 1
>
>       | i > 1            = fib (i-1) + fib (i-2)
>       | otherwise        = 0
>
> testWhere = {-# SCC "Where" #-} res
>   where res = fib 35
>
> testLet = {-# SCC "Let" #-}
>           let res = fib 35
>           in res
>

>
> Can anyone please tell me, if this is the expected behavior?

I think so.
testWhere is equivalent to

testLet2 = let res = fib35 in {-# SCC "Where" #-} res

so the cost centre covers only the result, not the computation.
To get the computation into the cost centre, use

testWhere = res
  where
    res = {-# SCC "Where" #-} fib 35

which corresponds to

let res = {-# SCC "Whatever" #-} fib 35 in res

> I'm not
> very experienced in benchmarking and was a bit irritated by that
> difference.
>
> Cheers, Daniel.

Ditto ;)


More information about the Beginners mailing list