Fib.hs

-- | Parallelism
-- An examples on how to speed up computations by doing things in parallel
-- Functional Programming course 2018.
-- Thomas Hallgren

{-
This started as a skeleton, the definitions were filled in
during the lecture.
-}

import Control.Parallel

main = print (fib 43)

fib :: Int -> Int
fib 0 = 1
fib 1 = 1
fib n | n<34 = fib (n-1) + fib (n-2)
fib n = par f1 (par f2 (f1+f2))
  where
    f1 = fib (n-1)
    f2 = fib (n-2)
{-
-- | Inefficient computation of Fibonacci numbers
fib :: Int -> Int
fib 0 = 1
fib 1 = 1
fib n = fib (n-1) + fib (n-2)
-}


{- We tested this by compiling the program and timing the execution:

ghc -O -threaded Fib.hs
time ./Fib
time ./Fib +RTS -N2
time ./Fib +RTS -N4
time ./Fib +RTS -N8
 
-}

Plain-text version of Fib.hs | Valid HTML?