git.iko.soy/typeable/attoparsec
on master help sign in
Move number parsing benchmarks into their own module

--HG-- rename : benchmarks/Benchmarks.hs => benchmarks/Numbers.hs extra : rebase_source : 2f7bcff2cfe5158a8253a2400b00e1643df05255 extra : amend_source : 0354a0454d17f9128c34b882b28dac61e48afce1

commit7ec136c6772ce900464aa1013010bf894a4a69b7 authorBryan O'Sullivan <bos@serpentine.com> dateThu 8 May 2014, 21:12 (12 years ago) parent0a4e607
4 files changed+104−66
attoparsec.cabal+3 benchmarks/Benchmarks.hs+17−65 benchmarks/Numbers.hs+81 benchmarks/attoparsec-benchmarks.cabal+3−1
attoparsec.cabal+3
@@ -90,7 +90,10 @@ test-suite tests
9090benchmark benchmarks
9191 type: exitcode-stdio-1.0
9292 hs-source-dirs: benchmarks
93+ ghc-options: -O2 -Wall
9394 main-is: Benchmarks.hs
95+ other-modules:
96+ Numbers
9497 build-depends:
9598 attoparsec,
9699 base,
benchmarks/Benchmarks.hs+17−65
@@ -1,13 +1,14 @@
11{-# LANGUAGE BangPatterns, CPP #-}
2+{-# OPTIONS_GHC -fno-warn-orphans #-}
23
3−import Control.Applicative
4+import Control.Applicative (many)
45import Control.DeepSeq (NFData(rnf))
5−import Criterion.Main (bench, bgroup, defaultMain, nf, whnf)
6−import Data.Bits (unsafeShiftL)
7−import Data.ByteString.Internal (ByteString(..))
8−import Data.Char
9−import Data.Scientific (Scientific(..))
6+import Criterion.Main (bench, bgroup, defaultMain, nf)
7+import Data.Bits
8+import Data.Char (isAlpha)
109import Data.Word (Word32)
10+import Data.Word (Word8)
11+import Numbers (numbers)
1112import Text.Parsec.Text ()
1213import Text.Parsec.Text.Lazy ()
1314import qualified Data.Attoparsec.ByteString as AB
@@ -20,10 +21,10 @@ import qualified Data.ByteString.Char8 as BC
2021import qualified Data.ByteString.Lazy as BL
2122import qualified Data.Text as T
2223import qualified Data.Text.Lazy as TL
23−import Data.Word (Word8)
2424import qualified Text.Parsec as P
2525
2626#if !MIN_VERSION_bytestring(0,10,0)
27+import Data.ByteString.Internal (ByteString(..))
2728instance NFData ByteString where
2829 rnf (PS _ _ _) = ()
2930#endif
@@ -37,9 +38,7 @@ chunksOf k = go
3738 ([],_) -> []
3839 (y, ys) -> y : go ys
3940
40−fromLazy :: BL.ByteString -> B.ByteString
41−fromLazy = B.concat . BL.toChunks
42−
41+main :: IO ()
4342main = do
4443 let s = take 1024 . cycle $ ['a'..'z'] ++ ['A'..'Z']
4544 !b = BC.pack s
@@ -75,67 +74,15 @@ main = do
7574 , bgroup "takeWhile" [
7675 bench "isAlpha" $ nf (ABL.parse (AC.takeWhile isAlpha)) bl
7776 , bench "isAlpha_ascii" $ nf (ABL.parse (AC.takeWhile AC.isAlpha_ascii)) bl
78− , bench "isAlpha_iso8859_15" $ nf (ABL.parse (AC.takeWhile AC.isAlpha_iso8859_15)) bl
77+ , bench "isAlpha_iso8859_15" $
78+ nf (ABL.parse (AC.takeWhile AC.isAlpha_iso8859_15)) bl
7979 ]
8080 , bench "word32LE" $ nf (AB.parse word32LE) b
8181 , bgroup "scan" [
8282 bench "short" $ nf (AB.parse quotedString) (BC.pack "abcdefghijk\"")
8383 , bench "long" $ nf (AB.parse quotedString) b
8484 ]
85−
86− , let strN = "1234.56789"
87− strNePos = "1234.56789e3"
88− strNeNeg = "1234.56789e-3"
89− in
90− bgroup "numbers"
91− [ let !tN = T.pack strN
92− !tNePos = T.pack strNePos
93− !tNeNeg = T.pack strNeNeg
94− in bgroup "Text"
95− [
96− bgroup "no power"
97− [ bench "double" $ nf (AT.parseOnly AT.double) tN
98− , bench "number" $ nf (AT.parseOnly AT.number) tN
99− , bench "rational" $ nf (AT.parseOnly (AT.rational :: AT.Parser Rational)) tN
100− , bench "scientific" $ nf (AT.parseOnly (AT.rational :: AT.Parser Scientific)) tN
101− ]
102− , bgroup "positive power"
103− [ bench "double" $ nf (AT.parseOnly AT.double) tNePos
104− , bench "number" $ nf (AT.parseOnly AT.number) tNePos
105− , bench "rational" $ nf (AT.parseOnly (AT.rational :: AT.Parser Rational)) tNePos
106− , bench "scientific" $ nf (AT.parseOnly (AT.rational :: AT.Parser Scientific)) tNePos
107− ]
108− , bgroup "negative power"
109− [ bench "double" $ nf (AT.parseOnly AT.double) tNeNeg
110− , bench "number" $ nf (AT.parseOnly AT.number) tNeNeg
111− , bench "rational" $ nf (AT.parseOnly (AT.rational :: AT.Parser Rational)) tNeNeg
112− , bench "scientific" $ nf (AT.parseOnly (AT.rational :: AT.Parser Scientific)) tNeNeg
113− ]
114− ]
115− , let !bN = BC.pack strN
116− !bNePos = BC.pack strNePos
117− !bNeNeg = BC.pack strNeNeg
118− in bgroup "ByteString"
119− [ bgroup "no power"
120− [ bench "double" $ nf (AC.parseOnly AC.double) bN
121− , bench "number" $ nf (AC.parseOnly AC.number) bN
122− , bench "rational" $ nf (AC.parseOnly (AC.rational :: AC.Parser Rational)) bN
123− , bench "scientific" $ nf (AC.parseOnly (AC.rational :: AC.Parser Scientific)) bN
124− ]
125− , bgroup "positive power"
126− [ bench "double" $ nf (AC.parseOnly AC.double) bNePos
127− , bench "number" $ nf (AC.parseOnly AC.number) bNePos
128− , bench "rational" $ nf (AC.parseOnly (AC.rational :: AC.Parser Rational)) bNePos
129− , bench "scientific" $ nf (AC.parseOnly (AC.rational :: AC.Parser Scientific)) bNePos
130− ]
131− , bgroup "negative power"
132− [ bench "double" $ nf (AC.parseOnly AC.double) bNeNeg
133− , bench "number" $ nf (AC.parseOnly AC.number) bNeNeg
134− , bench "rational" $ nf (AC.parseOnly (AC.rational :: AC.Parser Rational)) bNeNeg
135− , bench "scientific" $ nf (AC.parseOnly (AC.rational :: AC.Parser Scientific)) bNeNeg
136− ]
137− ]
138− ]
85+ , numbers
13986 ]
14087
14188-- Benchmarks bind and (potential) bounds-check merging.
@@ -162,3 +109,8 @@ quotedString = AB.scan False $ \s c -> if s then Just False
162109 else if c == doubleQuote
163110 then Nothing
164111 else Just (c == backslash)
112+
113+#if !MIN_VERSION_base(4,5,0)
114+unsafeShiftL :: Bits a => a -> Int -> a
115+unsafeShiftL = shiftL
116+#endif
benchmarks/Numbers.hsnew file+81
@@ -0,0 +1,81 @@
1+{-# LANGUAGE BangPatterns #-}
2+
3+module Numbers (numbers) where
4+
5+import Criterion.Main (bench, bgroup, nf)
6+import Criterion.Types (Benchmark)
7+import Data.Scientific (Scientific(..))
8+import Text.Parsec.Text ()
9+import Text.Parsec.Text.Lazy ()
10+import qualified Data.Attoparsec.ByteString.Char8 as AC
11+import qualified Data.Attoparsec.Text as AT
12+import qualified Data.ByteString.Char8 as BC
13+import qualified Data.Text as T
14+
15+strN, strNePos, strNeNeg :: String
16+strN = "1234.56789"
17+strNePos = "1234.56789e3"
18+strNeNeg = "1234.56789e-3"
19+
20+numbers :: Benchmark
21+numbers = bgroup "numbers" [
22+ let !tN = T.pack strN
23+ !tNePos = T.pack strNePos
24+ !tNeNeg = T.pack strNeNeg
25+ in bgroup "Text"
26+ [
27+ bgroup "no power"
28+ [ bench "double" $ nf (AT.parseOnly AT.double) tN
29+ , bench "number" $ nf (AT.parseOnly AT.number) tN
30+ , bench "rational" $
31+ nf (AT.parseOnly (AT.rational :: AT.Parser Rational)) tN
32+ , bench "scientific" $
33+ nf (AT.parseOnly (AT.rational :: AT.Parser Scientific)) tN
34+ ]
35+ , bgroup "positive power"
36+ [ bench "double" $ nf (AT.parseOnly AT.double) tNePos
37+ , bench "number" $ nf (AT.parseOnly AT.number) tNePos
38+ , bench "rational" $
39+ nf (AT.parseOnly (AT.rational :: AT.Parser Rational)) tNePos
40+ , bench "scientific" $
41+ nf (AT.parseOnly (AT.rational :: AT.Parser Scientific)) tNePos
42+ ]
43+ , bgroup "negative power"
44+ [ bench "double" $ nf (AT.parseOnly AT.double) tNeNeg
45+ , bench "number" $ nf (AT.parseOnly AT.number) tNeNeg
46+ , bench "rational" $
47+ nf (AT.parseOnly (AT.rational :: AT.Parser Rational)) tNeNeg
48+ , bench "scientific" $
49+ nf (AT.parseOnly (AT.rational :: AT.Parser Scientific)) tNeNeg
50+ ]
51+ ]
52+ , let !bN = BC.pack strN
53+ !bNePos = BC.pack strNePos
54+ !bNeNeg = BC.pack strNeNeg
55+ in bgroup "ByteString"
56+ [ bgroup "no power"
57+ [ bench "double" $ nf (AC.parseOnly AC.double) bN
58+ , bench "number" $ nf (AC.parseOnly AC.number) bN
59+ , bench "rational" $
60+ nf (AC.parseOnly (AC.rational :: AC.Parser Rational)) bN
61+ , bench "scientific" $
62+ nf (AC.parseOnly (AC.rational :: AC.Parser Scientific)) bN
63+ ]
64+ , bgroup "positive power"
65+ [ bench "double" $ nf (AC.parseOnly AC.double) bNePos
66+ , bench "number" $ nf (AC.parseOnly AC.number) bNePos
67+ , bench "rational" $
68+ nf (AC.parseOnly (AC.rational :: AC.Parser Rational)) bNePos
69+ , bench "scientific" $
70+ nf (AC.parseOnly (AC.rational :: AC.Parser Scientific)) bNePos
71+ ]
72+ , bgroup "negative power"
73+ [ bench "double" $ nf (AC.parseOnly AC.double) bNeNeg
74+ , bench "number" $ nf (AC.parseOnly AC.number) bNeNeg
75+ , bench "rational" $
76+ nf (AC.parseOnly (AC.rational :: AC.Parser Rational)) bNeNeg
77+ , bench "scientific" $
78+ nf (AC.parseOnly (AC.rational :: AC.Parser Scientific)) bNeNeg
79+ ]
80+ ]
81+ ]
benchmarks/attoparsec-benchmarks.cabal+3−1
@@ -8,8 +8,10 @@ build-type: Simple
88
99executable attoparsec-benchmarks
1010 main-is: Benchmarks.hs
11+ other-modules:
12+ Numbers
1113 hs-source-dirs: .. .
12− ghc-options: -O2
14+ ghc-options: -O2 -Wall
1315 build-depends:
1416 array,
1517 base == 4.*,