@@ -1,12 +1,26 @@
11-- | Read numbers from a file with a just a number on each line, find the
2−-- minimum of those numbers.
2+-- minimum of those numbers. The file contains different kinds of numbers:
3+--
4+-- * Decimals
5+--
6+-- * Hexadecimals
7+--
8+-- * Floating point numbers
9+--
10+-- * Floating point numbers in scientific notation
11+--
12+-- The different benchmarks will only take into account the values they can
13+-- parse.
14+--
15+-- Tested in this benchmark:
16+--
17+-- * Lexing/parsing of different numerical types
318--
419module Data.Text.Benchmarks.ReadNumbers
520 ( benchmark
621 ) where
722
8−import Control.Exception (evaluate)
9−import Criterion (Benchmark, bgroup, bench)
23+import Criterion (Benchmark, bgroup, bench, whnf)
1024import Data.List (foldl')
1125import Numeric (readDec, readFloat, readHex)
1226import qualified Data.ByteString.Char8 as B
@@ -14,49 +28,48 @@ import qualified Data.ByteString.Lazy.Char8 as BL
1428import qualified Data.ByteString.Lex.Double as B
1529import qualified Data.ByteString.Lex.Lazy.Double as BL
1630import qualified Data.Text as T
17−import qualified Data.Text.Encoding as T
31+import qualified Data.Text.IO as T
1832import qualified Data.Text.Lazy as TL
19−import qualified Data.Text.Lazy.Encoding as TL
33+import qualified Data.Text.Lazy.IO as TL
2034import qualified Data.Text.Lazy.Read as TL
2135import qualified Data.Text.Read as T
2236
2337benchmark :: FilePath -> IO Benchmark
24−benchmark fp = return $ bgroup "ReadNumbers"
25− [ bench "DecimalString" $ readFile fp >>= evaluate .
26− int . string readDec . lines
27− , bench "HexadecimalString" $ readFile fp >>= evaluate .
28− int . string readHex . lines
29− , bench "DoubleString" $ readFile fp >>= evaluate .
30− double . string readFloat . lines
38+benchmark fp = do
39+ -- Read all files into lines: string, text, lazy text, bytestring, lazy
40+ -- bytestring
41+ s <- lines `fmap` readFile fp
42+ t <- T.lines `fmap` T.readFile fp
43+ tl <- TL.lines `fmap` TL.readFile fp
44+ b <- B.lines `fmap` B.readFile fp
45+ bl <- BL.lines `fmap` BL.readFile fp
46+ return $ bgroup "ReadNumbers"
47+ [ bench "DecimalString" $ whnf (int . string readDec) s
48+ , bench "HexadecimalString" $ whnf (int . string readHex) s
49+ , bench "DoubleString" $ whnf (double . string readFloat) s
3150
32− , bench "DecimalText" $ B.readFile fp >>= evaluate .
33− int . text (T.signed T.decimal) . T.lines . T.decodeUtf8
34− , bench "HexadecimalText" $ B.readFile fp >>= evaluate .
35− int . text (T.signed T.hexadecimal) . T.lines . T.decodeUtf8
36− , bench "DoubleText" $ B.readFile fp >>= evaluate .
37− double . text T.double . T.lines . T.decodeUtf8
38− , bench "RationalText" $ B.readFile fp >>= evaluate .
39− double . text T.rational . T.lines . T.decodeUtf8
51+ , bench "DecimalText" $ whnf (int . text (T.signed T.decimal)) t
52+ , bench "HexadecimalText" $ whnf (int . text (T.signed T.hexadecimal)) t
53+ , bench "DoubleText" $ whnf (double . text T.double) t
54+ , bench "RationalText" $ whnf (double . text T.rational) t
4055
41− , bench "DecimalLazyText" $ BL.readFile fp >>= evaluate .
42− int . text (TL.signed TL.decimal) . TL.lines . TL.decodeUtf8
43− , bench "HexadecimalLazyText" $ BL.readFile fp >>= evaluate .
44− int . text (TL.signed TL.hexadecimal) . TL.lines . TL.decodeUtf8
45− , bench "DoubleLazyText" $ BL.readFile fp >>= evaluate .
46− double . text TL.double . TL.lines . TL.decodeUtf8
47− , bench "RationalLazyText" $ BL.readFile fp >>= evaluate .
48− double . text TL.rational . TL.lines . TL.decodeUtf8
56+ , bench "DecimalLazyText" $
57+ whnf (int . text (TL.signed TL.decimal)) tl
58+ , bench "HexadecimalLazyText" $
59+ whnf (int . text (TL.signed TL.hexadecimal)) tl
60+ , bench "DoubleLazyText" $
61+ whnf (double . text TL.double) tl
62+ , bench "RationalLazyText" $
63+ whnf (double . text TL.rational) tl
4964
50− , bench "DecimalByteString" $ B.readFile fp >>= evaluate .
51− int . byteString B.readInt . B.lines
52− , bench "DoubleByteString" $ B.readFile fp >>= evaluate .
53− double . byteString B.readDouble . B.lines
65+ , bench "DecimalByteString" $ whnf (int . byteString B.readInt) b
66+ , bench "DoubleByteString" $ whnf (double . byteString B.readDouble) b
5467
55− , bench "DecimalLazyByteString" $ BL.readFile fp >>= evaluate .
56− int . byteString BL.readInt . BL.lines
57− , bench "DoubleLazyByteString" $ BL.readFile fp >>= evaluate .
58− double . byteString BL.readDouble . BL.lines
59− ]
68+ , bench "DecimalLazyByteString" $
69+ whnf (int . byteString BL.readInt) bl
70+ , bench "DoubleLazyByteString" $
71+ whnf (double . byteString BL.readDouble) bl
72+ ]
6073 where
6174 -- Used for fixing types
6275 int :: Int -> Int