@@ -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