-
Notifications
You must be signed in to change notification settings - Fork 27
/
Simple.hs
104 lines (86 loc) · 3.01 KB
/
Simple.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
{-# LANGUAGE BangPatterns, OverloadedStrings #-}
--module Main (main) where
import Control.Monad
import Data.Char
import Data.Bits
import System.Environment
import Data.Text.Format as T
import Data.Time.Clock
import Data.Text (Text)
import qualified Data.Text as T
import Data.Text.Lazy.Encoding
import qualified Data.ByteString.Lazy as L
import System.IO
counting :: Int -> (Int -> () -> IO ()) -> IO ()
counting count act = loop 0
where loop !i | i < count = act i () >> loop (i+1)
| otherwise = return ()
{-# NOINLINE counting #-}
idle count = counting count $ \_ x -> return ()
plain count = counting count $ \_ x -> do
L.putStr . encodeUtf8 $ "hi mom\n"
unit count = counting count $ \_ x -> do
let t = T.format "hi mom\n" x
L.putStr . encodeUtf8 $ t
int count = counting count $ \i x -> do
let t = T.format "hi mom {}\n" (Only i)
L.putStr . encodeUtf8 $ t
bigint count = counting count $ \i x -> do
let t = T.format "hi mom {}\n" (Only (i+100000))
L.putStr . encodeUtf8 $ t
double count = counting count $ \i x -> do
let t = T.format "hi mom {}\n" (Only (fromIntegral i * dpi))
L.putStr . encodeUtf8 $ t
p6 count = counting count $ \i x -> do
let t = T.format "hi mom {}\n" (Only (prec 6 $! fromIntegral i * dpi))
L.putStr . encodeUtf8 $ t
arg :: Int -> Text
arg i = "fnord" `T.append` (T.take (i `mod` 6) "foobar")
{-# NOINLINE arg #-}
one count = counting count $ \i x -> do
let k = arg i
let t = {-# SCC "one/format" #-} T.format "hi mom {}\n" (Only k)
L.putStr . encodeUtf8 $ t
two count = counting count $ \i x -> do
let k = arg i
let t = {-# SCC "two/format" #-} T.format "hi mom {} {}\n" (k,k)
L.putStr . encodeUtf8 $ t
three count = counting count $ \i x -> do
let k = arg i
let t = {-# SCC "three/format" #-} T.format "hi mom {} {} {}\n" (k,k,k)
L.putStr . encodeUtf8 $ t
four count = counting count $ \i x -> do
let k = arg i
let t = {-# SCC "four/format" #-} T.format "hi mom {} {} {} {}\n" (k,k,k,k)
L.putStr . encodeUtf8 $ t
five count = counting count $ \i x -> do
let k = arg i
let t = {-# SCC "five/format" #-} T.format "hi mom {} {} {} {} {}\n" (k,k,k,k,k)
L.putStr . encodeUtf8 $ t
dpi :: Double
dpi = pi
main = do
args <- getArgs
let count = case args of
(_:x:_) -> read x
_ -> 100000
let bm = case args of
("idle":_) -> idle
("plain":_) -> plain
("unit":_) -> unit
("double":_) -> double
("p6":_) -> p6
("int":_) -> int
("bigint":_) -> bigint
("one":_) -> one
("two":_) -> two
("three":_) -> three
("four":_) -> four
("five":_) -> five
_ -> error "wut?"
start <- getCurrentTime
bm count
elapsed <- (`diffUTCTime` start) `fmap` getCurrentTime
T.hprint stderr "{} iterations in {} secs ({} thousand/sec)\n"
(count, elapsed,
fromRational (toRational count / toRational elapsed / 1e3) :: Double)