on master help sign in
Compare number reading performance to ByteString functions.
commit910a58a6ad5a73e7f387baccf5a8379b6a314dd9 authorBryan O'Sullivan <bos@serpentine.com> dateThu 21 Oct 2010, 22:13 (16 years ago) parenteedca06
1 file changed+16−1
tests/benchmarks/ReadNumbers.hs+16−1
tests/benchmarks/ReadNumbers.hs+16−1
@@ -1,9 +1,11 @@
11{-# LANGUAGE BangPatterns #-}
2−import Control.Monad ()
2+
33import Data.List (foldl')
44import System.Environment (getArgs)
55import qualified Data.ByteString.Char8 as B
66import 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
79import qualified Data.Text as T
810import qualified Data.Text.Encoding as T
911import qualified Data.Text.Lazy as TL
@@ -18,10 +20,19 @@ readem act reader = print . foldl' go 1000000 =<< act
1820 Left err -> error err
1921 Right (n,_) -> min n z
2022
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+
2130main = do
2231 args <- getArgs
2332 let strict = (T.lines . T.decodeUtf8) `fmap` B.getContents
2433 lazy = (TL.lines . TL.decodeUtf8) `fmap` BL.getContents
34+ bs = B.lines `fmap` B.getContents
35+ lbs = BL.lines `fmap` BL.getContents
2536 case args of
2637 ["dec"] -> readem strict (T.signed T.decimal :: T.Reader Int)
2738 ["hex"] -> readem strict (T.signed T.hexadecimal :: T.Reader Int)
@@ -31,3 +42,7 @@ main = do
3142 ["lhex"] -> readem lazy (TL.signed TL.hexadecimal :: TL.Reader Int)
3243 ["ldouble"] -> readem lazy (TL.double :: TL.Reader Double)
3344 ["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