on master help sign in
Compare number reading performance to ByteString functions.
910a58aBryan O'Sullivan
tests/benchmarks/ReadNumbers.hs+16−1
@@ -1,9 +1,11 @@
1{-# LANGUAGE BangPatterns #-}
1{-# LANGUAGE BangPatterns #-}
2−import Control.Monad ()
2+
3import Data.List (foldl')
3import Data.List (foldl')
4import System.Environment (getArgs)
4import System.Environment (getArgs)
5import qualified Data.ByteString.Char8 as B
5import qualified Data.ByteString.Char8 as B
6import qualified Data.ByteString.Lazy.Char8 as BL
6import qualified Data.ByteString.Lazy.Char8 as BL
7+import qualified Data.ByteString.Lex.Double as B
8+import qualified Data.ByteString.Lex.Lazy.Double as BL
7import qualified Data.Text as T
9import qualified Data.Text as T
8import qualified Data.Text.Encoding as T
10import qualified Data.Text.Encoding as T
9import qualified Data.Text.Lazy as TL
11import qualified Data.Text.Lazy as TL
@@ -18,10 +20,19 @@ readem act reader = print . foldl' go 1000000 =<< act
18 Left err -> error err
20 Left err -> error err
19 Right (n,_) -> min n z
21 Right (n,_) -> min n z
20
22
23+bytey :: (Ord a, Num a) =>
24+ IO [t] -> (t -> Maybe (a,t)) -> IO ()
25+bytey act reader = print . foldl' go 1000000 =<< act
26+ where go z t = case reader t of
27+ Nothing -> error "barf"
28+ Just (n,_) -> min n z
29+
21main = do
30main = do
22 args <- getArgs
31 args <- getArgs
23 let strict = (T.lines . T.decodeUtf8) `fmap` B.getContents
32 let strict = (T.lines . T.decodeUtf8) `fmap` B.getContents
24 lazy = (TL.lines . TL.decodeUtf8) `fmap` BL.getContents
33 lazy = (TL.lines . TL.decodeUtf8) `fmap` BL.getContents
34+ bs = B.lines `fmap` B.getContents
35+ lbs = BL.lines `fmap` BL.getContents
25 case args of
36 case args of
26 ["dec"] -> readem strict (T.signed T.decimal :: T.Reader Int)
37 ["dec"] -> readem strict (T.signed T.decimal :: T.Reader Int)
27 ["hex"] -> readem strict (T.signed T.hexadecimal :: T.Reader Int)
38 ["hex"] -> readem strict (T.signed T.hexadecimal :: T.Reader Int)
@@ -31,3 +42,7 @@ main = do
31 ["lhex"] -> readem lazy (TL.signed TL.hexadecimal :: TL.Reader Int)
42 ["lhex"] -> readem lazy (TL.signed TL.hexadecimal :: TL.Reader Int)
32 ["ldouble"] -> readem lazy (TL.double :: TL.Reader Double)
43 ["ldouble"] -> readem lazy (TL.double :: TL.Reader Double)
33 ["lrational"] -> readem lazy (TL.rational :: TL.Reader Double)
44 ["lrational"] -> readem lazy (TL.rational :: TL.Reader Double)
45+ ["bdec"] -> bytey bs B.readInt
46+ ["bdouble"] -> bytey bs B.readDouble
47+ ["bldec"] -> bytey lbs BL.readInt
48+ ["bldouble"] -> bytey lbs BL.readDouble