-
Notifications
You must be signed in to change notification settings - Fork 0
/
Time.hs
140 lines (137 loc) · 4.57 KB
/
Time.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE ScopedTypeVariables #-}
module Main (main) where
import Control.Monad
import Criterion.Main
import Criterion.Types
import Data.Char
import Data.Scientific
import System.Directory
main :: IO ()
main = do
let fp = "out.csv"
exists <- doesFileExist fp
when exists (removeFile fp)
defaultMainWith
defaultConfig {csvFile = Just fp}
[ bgroup
"Addition"
(concat
[ [ bench ("Int:" ++ scale i) (whnf add'Int (-i))
| i <- [1, 10, 100, 1000, 10000, 100000, 1000000]
]
, [ bench ("Double:" ++ scale i) (whnf add'Double (-i))
| i <- [1, 10, 100, 1000, 10000, 100000, 1000000]
]
, [ bench ("Integer:" ++ scale i) (whnf add'Integer (-i))
| i <- [1, 10, 100, 1000, 10000, 100000, 1000000]
]
, [ bench ("Rational:" ++ scale i) (whnf add'Rational (-i))
| i <- [1, 10, 100, 1000, 10000, 100000, 1000000]
]
, [ bench ("Scientific:" ++ scale i) (whnf add'Scientific (-i))
| i <- [1, 10, 100, 1000, 10000, 100000, 1000000]
]
])
, bgroup
"Subtraction"
(concat
[ [ bench ("Int:" ++ scale i) (whnf subtract'Int i)
| i <- [1, 10, 100, 1000, 10000, 100000, 1000000]
]
, [ bench ("Double:" ++ scale i) (whnf subtract'Double i)
| i <- [1, 10, 100, 1000, 10000, 100000, 1000000]
]
, [ bench ("Integer:" ++ scale i) (whnf subtract'Integer i)
| i <- [1, 10, 100, 1000, 10000, 100000, 1000000]
]
, [ bench ("Rational:" ++ scale i) (whnf subtract'Rational i)
| i <- [1, 10, 100, 1000, 10000, 100000, 1000000]
]
, [ bench ("Scientific:" ++ scale i) (whnf subtract'Scientific i)
| i <- [1, 10, 100, 1000, 10000, 100000, 1000000]
]
])
, bgroup
"Integer division"
(concat
[ [ bench ("Int:" ++ scale i) (whnf div'Int i)
| i <- [1000, 10000, 100000, 1000000, 10000000]
]
, [ bench ("Integer:" ++ scale i) (whnf div'Integer i)
| i <- [1000, 10000, 100000, 1000000, 10000000]
]
])
, bgroup
"Decimal division"
(concat
[ [ bench ("Double:" ++ scale i) (whnf div'Double i)
| i <- [10, 100, 1000]
]
, [ bench ("Rational:" ++ scale i) (whnf div'Rational i)
| i <- [10, 100, 1000]
]
, [ bench ("Scientific:" ++ scale i) (whnf div'Scientific i)
| i <- [10, 100, 1000]
]
])
]
where
subtract'Integer :: Integer -> ()
subtract'Integer 0 = ()
subtract'Integer a = subtract'Integer (a - 1)
subtract'Int :: Int -> ()
subtract'Int 0 = ()
subtract'Int a = subtract'Int (a - 1)
subtract'Rational :: Rational -> ()
subtract'Rational 0 = ()
subtract'Rational a = subtract'Rational (a - 1)
subtract'Double :: Double -> ()
subtract'Double 0 = ()
subtract'Double a = subtract'Double (a - 1)
subtract'Scientific :: Scientific -> ()
subtract'Scientific 0 = ()
subtract'Scientific a = subtract'Scientific (a - 1)
add'Integer :: Integer -> ()
add'Integer 0 = ()
add'Integer a = add'Integer (a + 1)
add'Int :: Int -> ()
add'Int 0 = ()
add'Int a = add'Int (a + 1)
add'Rational :: Rational -> ()
add'Rational 0 = ()
add'Rational a = add'Rational (a + 1)
add'Double :: Double -> ()
add'Double 0 = ()
add'Double a = add'Double (a + 1)
add'Scientific :: Scientific -> ()
add'Scientific 0 = ()
add'Scientific a = add'Scientific (a + 1)
div'Rational :: Int -> Rational
div'Rational i = go i (fromIntegral i)
where
go 0 (!c) = c
go i (!c) = go (i - 1) (c / 2)
div'Scientific :: Int -> Scientific
div'Scientific i = go i (fromIntegral i)
where
go 0 (!c) = c
go i (!c) = go (i - 1) (c / 2)
div'Double :: Int -> Double
div'Double i = go i (fromIntegral i)
where
go 0 (!c) = c
go i (!c) = go (i - 1) (c / 2)
div'Int :: Int -> Int
div'Int i = go i (fromIntegral i)
where
go 0 (!c) = c
go i (!c) = go (i - 1) (div c 2)
div'Integer :: Int -> Integer
div'Integer i = go i (fromIntegral i)
where
go 0 (!c) = c
go i (!c) = go (i - 1) (div c 2)
-- | Show without any % 1 or .0 extensions.
scale :: Show a => a -> String
scale = takeWhile isDigit . show