@@ -1,12 +1,26 @@
1-- | Read numbers from a file with a just a number on each line, find the
1-- | 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:
8+-- * Floating point numbers
10+-- * Floating point numbers in scientific notation
12+-- The different benchmarks will only take into account the values they can
15+-- Tested in this benchmark:
17+-- * Lexing/parsing of different numerical types
4module Data.Text.Benchmarks.ReadNumbers
19module Data.Text.Benchmarks.ReadNumbers
5 ( benchmark
20 ( benchmark
8−import Control.Exception (evaluate)
9−import Criterion (Benchmark, bgroup, bench)
23+import Criterion (Benchmark, bgroup, bench, whnf)
10import Data.List (foldl')
24import Data.List (foldl')
11import Numeric (readDec, readFloat, readHex)
25import Numeric (readDec, readFloat, readHex)
12import qualified Data.ByteString.Char8 as B
26import qualified Data.ByteString.Char8 as B
@@ -14,49 +28,48 @@ import qualified Data.ByteString.Lazy.Char8 as BL
14import qualified Data.ByteString.Lex.Double as B
28import qualified Data.ByteString.Lex.Double as B
15import qualified Data.ByteString.Lex.Lazy.Double as BL
29import qualified Data.ByteString.Lex.Lazy.Double as BL
16import qualified Data.Text as T
30import qualified Data.Text as T
17−import qualified Data.Text.Encoding as T
31+import qualified Data.Text.IO as T
18import qualified Data.Text.Lazy as TL
32import qualified Data.Text.Lazy as TL
19−import qualified Data.Text.Lazy.Encoding as TL
33+import qualified Data.Text.Lazy.IO as TL
20import qualified Data.Text.Lazy.Read as TL
34import qualified Data.Text.Lazy.Read as TL
21import qualified Data.Text.Read as T
35import qualified Data.Text.Read as T
23benchmark :: FilePath -> IO Benchmark
37benchmark :: FilePath -> IO Benchmark
39+ -- Read all files into lines: string, text, lazy text, bytestring, lazy
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
24−benchmark fp = return $ bgroup "ReadNumbers"
46+ return $ bgroup "ReadNumbers"
25− [ bench "DecimalString" $ readFile fp >>= evaluate .
26− int . string readDec . lines
47+ [ bench "DecimalString" $ whnf (int . string readDec) s
27− , bench "HexadecimalString" $ readFile fp >>= evaluate .
48+ , bench "HexadecimalString" $ whnf (int . string readHex) s
28− int . string readHex . lines
29− , bench "DoubleString" $ readFile fp >>= evaluate .
30− double . string readFloat . lines
49+ , bench "DoubleString" $ whnf (double . string readFloat) s
32− , bench "DecimalText" $ B.readFile fp >>= evaluate .
33− int . text (T.signed T.decimal) . T.lines . T.decodeUtf8
51+ , bench "DecimalText" $ whnf (int . text (T.signed T.decimal)) t
34− , bench "HexadecimalText" $ B.readFile fp >>= evaluate .
35− int . text (T.signed T.hexadecimal) . T.lines . T.decodeUtf8
52+ , bench "HexadecimalText" $ whnf (int . text (T.signed T.hexadecimal)) t
36− , bench "DoubleText" $ B.readFile fp >>= evaluate .
37− double . text T.double . T.lines . T.decodeUtf8
53+ , bench "DoubleText" $ whnf (double . text T.double) t
38− , bench "RationalText" $ B.readFile fp >>= evaluate .
39− double . text T.rational . T.lines . T.decodeUtf8
54+ , bench "RationalText" $ whnf (double . text T.rational) t
41− , bench "DecimalLazyText" $ BL.readFile fp >>= evaluate .
56+ , bench "DecimalLazyText" $
42− int . text (TL.signed TL.decimal) . TL.lines . TL.decodeUtf8
57+ whnf (int . text (TL.signed TL.decimal)) tl
43− , bench "HexadecimalLazyText" $ BL.readFile fp >>= evaluate .
58+ , bench "HexadecimalLazyText" $
44− int . text (TL.signed TL.hexadecimal) . TL.lines . TL.decodeUtf8
59+ whnf (int . text (TL.signed TL.hexadecimal)) tl
45− , bench "DoubleLazyText" $ BL.readFile fp >>= evaluate .
60+ , bench "DoubleLazyText" $
46− double . text TL.double . TL.lines . TL.decodeUtf8
61+ whnf (double . text TL.double) tl
47− , bench "RationalLazyText" $ BL.readFile fp >>= evaluate .
62+ , bench "RationalLazyText" $
48− double . text TL.rational . TL.lines . TL.decodeUtf8
63+ whnf (double . text TL.rational) tl
50− , bench "DecimalByteString" $ B.readFile fp >>= evaluate .
51− int . byteString B.readInt . B.lines
65+ , bench "DecimalByteString" $ whnf (int . byteString B.readInt) b
52− , bench "DoubleByteString" $ B.readFile fp >>= evaluate .
53− double . byteString B.readDouble . B.lines
66+ , bench "DoubleByteString" $ whnf (double . byteString B.readDouble) b
55− , bench "DecimalLazyByteString" $ BL.readFile fp >>= evaluate .
68+ , bench "DecimalLazyByteString" $
56− int . byteString BL.readInt . BL.lines
69+ whnf (int . byteString BL.readInt) bl
57− , bench "DoubleLazyByteString" $ BL.readFile fp >>= evaluate .
70+ , bench "DoubleLazyByteString" $
58− double . byteString BL.readDouble . BL.lines
71+ whnf (double . byteString BL.readDouble) bl
61 -- Used for fixing types
74 -- Used for fixing types
62 int :: Int -> Int
75 int :: Int -> Int