-
Notifications
You must be signed in to change notification settings - Fork 3
/
Dynamic.hs
60 lines (42 loc) · 1.93 KB
/
Dynamic.hs
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
{-# OPTIONS_GHC -fcontext-stack=100 #-}
import Criterion.Main
import Criterion.Types
import Data.Monoid
import Data.TypeRep
import Data.TypeRep.Types.Basic
import Data.TypeRep.Types.Basic.Typeable ()
import qualified Data.Dynamic as Base -- For comparison
type Types = BoolType :+: IntType :+: ListType
type Types2 = CharType :+: CharType :+: CharType :+: CharType :+: CharType :+: CharType :+: CharType :+: CharType :+: CharType :+: CharType :+: CharType :+: CharType :+: CharType :+: CharType :+: CharType :+: CharType :+: CharType :+: CharType :+: CharType :+: CharType :+: CharType :+: CharType :+: CharType :+: CharType :+: CharType :+: CharType :+: CharType :+: CharType :+: BoolType :+: IntType
-- 30 terms
dynList :: Int -> [Dynamic Types]
dynList n = concat [[toDyn i, toDyn (even i)] | i <- [0..n]]
dynList2 :: Int -> [Dynamic Types2]
dynList2 n = concat [[toDyn i, toDyn (even i)] | i <- [0..n]]
dynListBase :: Int -> [Base.Dynamic]
dynListBase n = concat [[Base.toDyn i, Base.toDyn (even i)] | i <- [0..n]]
dynSum :: [Dynamic Types] -> Int
dynSum ds = sum [i | d <- ds, Right i <- [fromDyn d]]
dynSum2 :: [Dynamic Types2] -> Int
dynSum2 ds = sum [i | d <- ds, Right i <- [fromDyn d]]
dynSumBase :: [Base.Dynamic] -> Int
dynSumBase ds = sum [i | d <- ds, Just i <- [Base.fromDynamic d]]
testDyn :: Int -> Int
testDyn = dynSum . dynList
testDyn2 :: Int -> Int
testDyn2 = dynSum2 . dynList2
testDynBase :: Int -> Int
testDynBase = dynSumBase . dynListBase
main :: IO ()
main = defaultMainWith (defaultConfig {csvFile = Just "bench-results/dynamic.csv"})
[ bgroup "size=1000"
[ bench "testDyn" $ nf testDyn 1000
, bench "testDyn2" $ nf testDyn2 1000
, bench "testDynBase" $ nf testDynBase 1000
]
, bgroup "size=2000"
[ bench "testDyn" $ nf testDyn 2000
, bench "testDyn2" $ nf testDyn2 2000
, bench "testDynBase" $ nf testDynBase 2000
]
]