summaryrefslogtreecommitdiff
path: root/libraries/ghc-compact/tests/compact_bench.hs
blob: fa249dcc367f56aa35dd71c1aa5f3defe1664332 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import Control.Exception
import GHC.Compact
import qualified Data.Map as Map
import Data.Time.Clock
import Text.Printf
import System.Environment
import System.Mem
import Control.DeepSeq

-- Benchmark compact against compactWithSharing. e.g.
--   ./compact_bench 1000000

main = do
  [n] <- map read <$> getArgs
  let m = Map.fromList [(x,[x*1000..x*1000+10]) | x <- [1..(n::Integer)]]
  evaluate (force m)
  timeIt "compact" $ compact m >>= compactSize >>= print
  timeIt "compactWithSharing" $ compactWithSharing m >>= compactSize >>= print

timeIt :: String -> IO a -> IO a
timeIt str io = do
  performMajorGC
  t0 <- getCurrentTime
  a <- io
  t1 <- getCurrentTime
  printf "%s: %.2f\n" str (realToFrac (t1 `diffUTCTime` t0) :: Double)
  return a