128d79fed2d1aab299e774a6ec4a1bd2603277cc
[packages/binary.git] / tests / Benchmark.hs
1 module Main where
2
3 import qualified Data.ByteString.Lazy as L
4 import Data.Binary
5 import Data.Binary.Put
6
7 import Control.Exception
8 import System.CPUTime
9
10
11 main :: IO ()
12 main = do
13 word8
14 word16
15 word32
16 word64
17
18 time :: String -> IO a -> IO a
19 time label f = do
20 putStr (label ++ " ")
21 start <- getCPUTime
22 v <- f
23 end <- getCPUTime
24 let diff = (fromIntegral (end - start)) / (10^12)
25 print (diff :: Double)
26 return v
27
28 test label f n fs s = time label $ do
29 let bs = runPut (doN n fs s f)
30 evaluate (L.length bs)
31 return ()
32
33 doN 0 _ _ _ = return ()
34 doN !n !f !s !body = do
35 body s
36 doN (n-1) f (f s) body
37
38 word8 = test "Word8 1MB" putWord8 1000000 (+1) 0
39 word16 = test "Word16 1MB" putWord16be 500000 (+1) 0
40 word32 = test "Word32 1MB" putWord32be 250000 (+1) 0
41 word64 = test "Word64 1MB" putWord64be 125000 (+1) 0
42