Few days ago I was stuck at some issue:
I wanted to do some profiling of ordinary fibonacci function written in Haskell but I didn’t wanted to display the result (using main function, from Main module). Guys from #haskell irc channel (irc.freenode.net) were very helpful and they guided me to evaluate and rnf
module Main where import Control.Parallel.Strategies import Control.Exception fib :: Int -> Int fib 0 = 0 fib 1 = 1 fib n = fib(n - 1) + fib(n - 2) main = do evaluate(rnf (fib 32))
or, using function composition:
module Main where import Control.Parallel.Strategies import Control.Exception fib :: Int -> Int fib 0 = 0 fib 1 = 1 fib n = fib(n - 1) + fib(n - 2) eval = evaluate . rnf main = do eval(fib 32)
ghc --make Fib.hs time ./Fib
and for better performance:
ghc --make -O2 Fib.hs time ./Fib
Advertisement
May 2, 2010 at 12:18 am |
what do exactly rnf?
May 2, 2010 at 6:04 am |
rnf is forcing evaluation (since haskell is lazy, this might not happen by default and we need that in order to eliminate the stack space )