As others have said, this is due to lazy evaluation. To force evaluation you should use the deepseq package 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 the
...
of functions you defined above). When you post code for questions please make sure it runs (iow, you should include imports)
- The use of
rnf
from deepseq
. This forces the evaluation of each element in the list.
- The bang pattern on
!fibonaccimap
, meaning "do this now, don't wait". This forces the list to be evaluated to weak-head normal form (whnf, basically just the first constructor (:)
). Without this the rnf
function would itself remain unevaluated.
Resulting in:
$ ghc --make ds.hs
$ ./ds
Computation time fibonaccimap: 6.603 sec
If you're intending to do benchmarking you should also use optimization (-O2
) and the Criterion package instead of getCPUTime
.