Skip to content

Instantly share code, notes, and snippets.

@malfet
Created September 29, 2012 18:01
Show Gist options
  • Save malfet/3804734 to your computer and use it in GitHub Desktop.
Save malfet/3804734 to your computer and use it in GitHub Desktop.
Measuring 123456! evaluation time using various language constructs
import Text.Printf
import Control.Exception
import System.CPUTime
-- time function implementation borrowed from
-- http://www.haskell.org/haskellwiki/Timing_computations
time :: IO t -> IO t
time a = do
start <- getCPUTime
v <- a
end <- getCPUTime
let diff = (fromIntegral (end - start)) / (10^12)
printf "Computation time: %0.3f sec\n" (diff :: Double)
return v
-- Factorial implementations are from
-- "The Evolution of a Haskell Programmer"
-- http://www.willamette.edu/~fruehr/haskell/evolution.html
fac 0 = 1
fac n = n * fac (n-1)
lim :: Integer
lim = 123456
main = do
printf "Computing product [1..%d]\n" lim
time $ product [1..lim] `seq` return ()
printf "Computing fac %d\n" lim
time $ fac lim `seq` return ()
printf "Computing foldr (*) [1..%d]\n" lim
time $ foldr (*) 1 [1..lim] `seq` return ()
printf "Computing foldl (*) [1..%d]\n" lim
time $ foldl (*) 1 [1..lim] `seq` return ()
-- Below are programm output on 2.4Ghz Core i7
-- $ ghc-7.4.2 -O3 facbench.hs ; ./facbench
-- [1 of 1] Compiling Main ( facbench.hs, facbench.o )
-- Linking facbench ...
-- Computing product [1..123456]
-- Computation time: 1.726 sec
-- Computing fac 123456
-- Computation time: 2.028 sec
-- Computing foldr (*) [1..123456]
-- Computation time: 7.270 sec
-- Computing foldl (*) [1..123456]
-- Computation time: 1.711 sec
@mrklein
Copy link

mrklein commented Sep 30, 2012

Maybe my problem was that I did not use optimization flags.

import Text.Printf
import Control.Exception
import System.CPUTime

-- time function implementation borrowed from 
-- http://www.haskell.org/haskellwiki/Timing_computations 
time :: IO t -> IO t
time a = do
    start <- getCPUTime
    v <- a
    end   <- getCPUTime
    let diff = (fromIntegral (end - start)) / (10^12)
    printf "Computation time: %0.3f sec\n" (diff :: Double)
    return v

fac 1 = 1
fac n = n * fac (n - 1)

fac1 n = i_fac1 n 1
    where
        i_fac1 n acc | n == 1 = acc
                     | otherwise = i_fac1 (n - 1) (n * acc)

fac2 n = i_fac2 n 1
   where
       i_fac2 n acc =
          if n == 1 then
         acc
      else
         i_fac2 (n - 1) (n * acc)

lim :: Integer
lim = 123456

main :: IO()
main = do
   printf "Computing fac %d\n" lim
   time $ fac lim `seq` return ()
   printf "Computing fac1 %d\n" lim
   time $ fac1 lim `seq` return ()
   printf "Computing fac2 %d\n" lim
   time $ fac2 lim `seq` return ()
   printf "Computing product [1..%d]\n" lim
   time $ product [1..lim] `seq` return ()
   printf "Computing foldr (*) 1 [1..%d]\n" lim
   time $ foldr (*) 1 [1..lim] `seq` return ()
   printf "Computing foldl (*) 1 [1..%d]\n" lim
   time $ foldl (*) 1 [1..lim] `seq` return ()

-- alexey at daphne in ~$ ghc-7.4.1 test.hs 
-- [1 of 1] Compiling Main             ( test.hs, test.o )
-- Linking test ...
-- alexey at daphne in ~$ ./test 
-- Computing fac 123456
-- Computation time: 7.971 sec
-- Computing fac1 123456
-- Computation time: 9.099 sec
-- Computing fac2 123456
-- Computation time: 9.066 sec
-- Computing product [1..123456]
-- Computation time: 9.258 sec
-- Computing foldr (*) 1 [1..123456]
-- Computation time: 9.083 sec
-- Computing foldl (*) 1 [1..123456]
-- Computation time: 9.231 sec

-- alexey at daphne in ~$ ghc-7.4.1 -O3 test.hs 
-- [1 of 1] Compiling Main             ( test.hs, test.o )
-- Linking test ...
-- alexey at daphne in ~$ ./test 
-- Computing fac 123456
-- Computation time: 2.568 sec
-- Computing fac1 123456
-- Computation time: 2.317 sec
-- Computing fac2 123456
-- Computation time: 2.325 sec
-- Computing product [1..123456]
-- Computation time: 2.097 sec
-- Computing foldr (*) 1 [1..123456]
-- Computation time: 9.027 sec
-- Computing foldl (*) 1 [1..123456]
-- Computation time: 2.131 sec

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment