on master help sign in
Purify ReadNumbers benchmark
commit0a013a166aaf0c2a27cb9813ce685c257b1064ba authorJasper Van der Jeugt <jaspervdj@gmail.com> dateThu 9 Jun 2011, 10:54 (15 years ago) parentab50080
1 file changed+48−37
tests/benchmarks/src/Data/Text/Benchmarks/ReadNumbers.hs+48−37
tests/benchmarks/src/Data/Text/Benchmarks/ReadNumbers.hs+48−37
@@ -1,12 +1,22 @@
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.
314--
415module Data.Text.Benchmarks.ReadNumbers
516 ( benchmark
617 ) where
718
8−import Control.Exception (evaluate)
9−import Criterion (Benchmark, bgroup, bench)
19+import Criterion (Benchmark, bgroup, bench, whnf)
1020import Data.List (foldl')
1121import Numeric (readDec, readFloat, readHex)
1222import qualified Data.ByteString.Char8 as B
@@ -14,49 +24,50 @@ import qualified Data.ByteString.Lazy.Char8 as BL
1424import qualified Data.ByteString.Lex.Double as B
1525import qualified Data.ByteString.Lex.Lazy.Double as BL
1626import qualified Data.Text as T
17−import qualified Data.Text.Encoding as T
27+import qualified Data.Text.IO as T
1828import qualified Data.Text.Lazy as TL
19−import qualified Data.Text.Lazy.Encoding as TL
29+import qualified Data.Text.Lazy.IO as TL
2030import qualified Data.Text.Lazy.Read as TL
2131import qualified Data.Text.Read as T
2232
2333benchmark :: 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
34+benchmark fp = do
35+ -- Read all files into lines: string, text, lazy text, bytestring, lazy
36+ -- bytestring
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
42+ return $ bgroup "ReadNumbers"
43+ [ bench "DecimalString" $ whnf (int . string readDec) s
44+ , bench "HexadecimalString" $ whnf (int . string readHex) s
45+ , bench "DoubleString" $ whnf (double . string readFloat) s
3146
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
47+ , bench "DecimalText" $ whnf (int . text (T.signed T.decimal)) t
48+ , bench "HexadecimalText" $ whnf (int . text (T.signed T.hexadecimal)) t
49+ , bench "DoubleText" $ whnf (double . text T.double) t
50+ , bench "RationalText" $ whnf (double . text T.rational) t
4051
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
52+ , bench "DecimalLazyText" $
53+ whnf (int . text (TL.signed TL.decimal)) tl
54+ , bench "HexadecimalLazyText" $
55+ whnf (int . text (TL.signed TL.hexadecimal)) tl
56+ , bench "DoubleLazyText" $
57+ whnf (double . text TL.double) tl
58+ , bench "RationalLazyText" $
59+ whnf (double . text TL.rational) tl
4960
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
61+ , bench "DecimalByteString" $
62+ whnf (int . byteString B.readInt) b
63+ , bench "DoubleByteString" $
64+ whnf (double . byteString B.readDouble) b
5465
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− ]
66+ , bench "DecimalLazyByteString" $
67+ whnf (int . byteString BL.readInt) bl
68+ , bench "DoubleLazyByteString" $
69+ whnf (double . byteString BL.readDouble) bl
70+ ]
6071 where
6172 -- Used for fixing types
6273 int :: Int -> Int