on builder help sign in
text/tests/benchmarks/ReadNumbers.hs
48 lines 2.0 kB history raw
1{-# LANGUAGE BangPatterns #-}
3import Data.List (foldl')
4import System.Environment (getArgs)
5import qualified Data.ByteString.Char8 as B
6import qualified Data.ByteString.Lazy.Char8 as BL
7import qualified Data.ByteString.Lex.Double as B
8import qualified Data.ByteString.Lex.Lazy.Double as BL
9import qualified Data.Text as T
10import qualified Data.Text.Encoding as T
11import qualified Data.Text.Lazy as TL
12import qualified Data.Text.Lazy.Encoding as TL
13import qualified Data.Text.Lazy.Read as TL
14import qualified Data.Text.Read as T
16readem :: (Ord a, Num a) =>
17 IO [t] -> (t -> Either String (a,t)) -> IO ()
18readem act reader = print . foldl' go 1000000 =<< act
19 where go z t = case reader t of
20 Left err -> error err
21 Right (n,_) -> min n z
23bytey :: (Ord a, Num a) =>
24 IO [t] -> (t -> Maybe (a,t)) -> IO ()
25bytey 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
30main = do
31 args <- getArgs
32 let strict = (T.lines . T.decodeUtf8) `fmap` B.getContents
33 lazy = (TL.lines . TL.decodeUtf8) `fmap` BL.getContents
34 bs = B.lines `fmap` B.getContents
35 lbs = BL.lines `fmap` BL.getContents
36 case args of
37 ["dec"] -> readem strict (T.signed T.decimal :: T.Reader Int)
38 ["hex"] -> readem strict (T.signed T.hexadecimal :: T.Reader Int)
39 ["double"] -> readem strict (T.double :: T.Reader Double)
40 ["rational"] -> readem strict (T.rational :: T.Reader Double)
41 ["ldec"] -> readem lazy (TL.signed TL.decimal :: TL.Reader Int)
42 ["lhex"] -> readem lazy (TL.signed TL.hexadecimal :: TL.Reader Int)
43 ["ldouble"] -> readem lazy (TL.double :: 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