As others have said, this is due to lazy appreciation. For a forced evaluation, you should use deepseq and BangPatterns :
{-# LANGUAGE BangPatterns #-} import Control.DeepSeq import Text.Printf import System.CPUTime main :: IO () main = do iniciofibonaccimap <- getCPUTime let !fibonaccimap = rnf $ map fib listaVintesete fimfibonaccimap <- getCPUTime let difffibonaccimap = (fromIntegral (fimfibonaccimap - iniciofibonaccimap)) / (10^12) printf "Computation time fibonaccimap: %0.3f sec\n" (difffibonaccimap :: Double) ...
In the above code you should notice three things:
- It compiles (modulo
... functions you defined above). When you submit a code for questions, please make sure that it works (for example, you must include import). - Using
rnf from deepseq . This forces you to evaluate each item in the list. - Figure bang on
!fibonaccimap , which means "do it now, don't wait." This leads to the fact that the list will be evaluated in the form of a weak head (basically, only the first constructor (:) ). Without this, the rnf function alone will be invaluable.
Result:
$ ghc --make ds.hs $ ./ds Computation time fibonaccimap: 6.603 sec
If you intend to benchmark, you should also use optimization ( -O2 ) and Criterion instead of getCPUTime .
source share