@@ -1,12 +1,22 @@
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
4module Data.Text.Benchmarks.ReadNumbers
15module Data.Text.Benchmarks.ReadNumbers
5 ( benchmark
16 ( benchmark
8−import Control.Exception (evaluate)
9−import Criterion (Benchmark, bgroup, bench)
19+import Criterion (Benchmark, bgroup, bench, whnf)
10import Data.List (foldl')
20import Data.List (foldl')
11import Numeric (readDec, readFloat, readHex)
21import Numeric (readDec, readFloat, readHex)
12import qualified Data.ByteString.Char8 as B
22import qualified Data.ByteString.Char8 as B
@@ -14,49 +24,50 @@ import qualified Data.ByteString.Lazy.Char8 as BL
14import qualified Data.ByteString.Lex.Double as B
24import qualified Data.ByteString.Lex.Double as B
15import qualified Data.ByteString.Lex.Lazy.Double as BL
25import qualified Data.ByteString.Lex.Lazy.Double as BL
16import qualified Data.Text as T
26import qualified Data.Text as T
17−import qualified Data.Text.Encoding as T
27+import qualified Data.Text.IO as T
18import qualified Data.Text.Lazy as TL
28import qualified Data.Text.Lazy as TL
19−import qualified Data.Text.Lazy.Encoding as TL
29+import qualified Data.Text.Lazy.IO as TL
20import qualified Data.Text.Lazy.Read as TL
30import qualified Data.Text.Lazy.Read as TL
21import qualified Data.Text.Read as T
31import qualified Data.Text.Read as T
23benchmark :: FilePath -> IO Benchmark
33benchmark :: FilePath -> IO Benchmark
35+ -- Read all files into lines: string, text, lazy text, bytestring, lazy
37+ s <- lines `fmap` readFile fp
38+ t <- T.lines `fmap` T.readFile fp
39+ tl <- TL.lines `fmap` TL.readFile fp
40+ b <- B.lines `fmap` B.readFile fp
41+ bl <- BL.lines `fmap` BL.readFile fp
24−benchmark fp = return $ bgroup "ReadNumbers"
42+ return $ bgroup "ReadNumbers"
25− [ bench "DecimalString" $ readFile fp >>= evaluate .
26− int . string readDec . lines
43+ [ bench "DecimalString" $ whnf (int . string readDec) s
27− , bench "HexadecimalString" $ readFile fp >>= evaluate .
44+ , bench "HexadecimalString" $ whnf (int . string readHex) s
28− int . string readHex . lines
29− , bench "DoubleString" $ readFile fp >>= evaluate .
30− double . string readFloat . lines
45+ , 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
47+ , 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
48+ , 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
49+ , bench "DoubleText" $ whnf (double . text T.double) t
38− , bench "RationalText" $ B.readFile fp >>= evaluate .
39− double . text T.rational . T.lines . T.decodeUtf8
50+ , bench "RationalText" $ whnf (double . text T.rational) t
41− , bench "DecimalLazyText" $ BL.readFile fp >>= evaluate .
52+ , bench "DecimalLazyText" $
42− int . text (TL.signed TL.decimal) . TL.lines . TL.decodeUtf8
53+ whnf (int . text (TL.signed TL.decimal)) tl
43− , bench "HexadecimalLazyText" $ BL.readFile fp >>= evaluate .
54+ , bench "HexadecimalLazyText" $
44− int . text (TL.signed TL.hexadecimal) . TL.lines . TL.decodeUtf8
55+ whnf (int . text (TL.signed TL.hexadecimal)) tl
45− , bench "DoubleLazyText" $ BL.readFile fp >>= evaluate .
56+ , bench "DoubleLazyText" $
46− double . text TL.double . TL.lines . TL.decodeUtf8
57+ whnf (double . text TL.double) tl
47− , bench "RationalLazyText" $ BL.readFile fp >>= evaluate .
58+ , bench "RationalLazyText" $
48− double . text TL.rational . TL.lines . TL.decodeUtf8
59+ whnf (double . text TL.rational) tl
50− , bench "DecimalByteString" $ B.readFile fp >>= evaluate .
61+ , bench "DecimalByteString" $
51− int . byteString B.readInt . B.lines
62+ whnf (int . byteString B.readInt) b
52− , bench "DoubleByteString" $ B.readFile fp >>= evaluate .
63+ , bench "DoubleByteString" $
53− double . byteString B.readDouble . B.lines
64+ whnf (double . byteString B.readDouble) b
55− , bench "DecimalLazyByteString" $ BL.readFile fp >>= evaluate .
66+ , bench "DecimalLazyByteString" $
56− int . byteString BL.readInt . BL.lines
67+ whnf (int . byteString BL.readInt) bl
57− , bench "DoubleLazyByteString" $ BL.readFile fp >>= evaluate .
68+ , bench "DoubleLazyByteString" $
58− double . byteString BL.readDouble . BL.lines
69+ whnf (double . byteString BL.readDouble) bl
61 -- Used for fixing types
72 -- Used for fixing types
62 int :: Int -> Int
73 int :: Int -> Int