packages feed

microbench-0.1: demo.hs

-- microbench, a tiny microbenchmarking library for Haskell.
-- Copyright (C) 2008 Evan Martin <martine@danga.com>

import Data.IORef
import Data.List (foldl')

import Microbench

b_sum, b_foldl, b_foldl' :: Int -> Int
b_sum    n = sum [1..n]
b_foldl  n = foldl (+) 0 [1..n]
b_foldl' n = foldl' (+) 0 [1..n]
b_ioref :: Int -> IO ()
b_ioref n = do
  counter <- newIORef 0
  mapM_ (\x -> modifyIORef counter (+x)) [1..n]
  readIORef counter
  return ()

main = do
  putStrLn $ "This demo benchmark measures various ways to sum a list "
          ++ "of integers."
  microbench "sum n ints" b_sum
  microbench "foldl (+) n ints" b_foldl
  microbench "foldl' (+) n ints" b_foldl'
  microbench "sum n ints with an ioref" b_ioref
  microbench "sum 1..10 with an ioref" (b_ioref 10)
  microbench "sum 1..100 with an ioref" (b_ioref 100)
  return ()