@@ -1,12 +1,12 @@
1−-- | Take the first 100 lines from the file, and count the number of lines equal
2−-- to those lines
1+-- | Compare a string with a copy of itself that is identical except
2+-- for the last character.
33--
44module Data.Text.Benchmarks.Equality
5− ( benchmark
5+ (
6+ benchmark
67 ) where
78
8−import Control.Exception (evaluate)
9−import Criterion (Benchmark, bgroup, bench)
9+import Criterion (Benchmark, bgroup, bench, whnf)
1010import qualified Data.ByteString.Char8 as B
1111import qualified Data.ByteString.Lazy.Char8 as BL
1212import qualified Data.Text as T
@@ -15,20 +15,16 @@ import qualified Data.Text.Lazy as TL
1515import qualified Data.Text.Lazy.Encoding as TL
1616
1717benchmark :: FilePath -> IO Benchmark
18−benchmark fp = return $ bgroup "Equality"
19− [ bench "Text" $
20− B.readFile fp >>= evaluate . func . T.lines . T.decodeUtf8
21− , bench "LazyText" $
22− BL.readFile fp >>= evaluate . func . TL.lines . TL.decodeUtf8
23− , bench "ByteString" $
24− B.readFile fp >>= evaluate . func . B.lines
25− , bench "LazyByteString" $
26− BL.readFile fp >>= evaluate . func . BL.lines
27− , bench "String" $
28− readFile fp >>= evaluate . func . lines
18+benchmark fp = do
19+ b <- B.readFile fp
20+ bl <- BL.readFile fp
21+ l <- readFile fp
22+ let t = T.decodeUtf8 b
23+ tl = TL.decodeUtf8 bl
24+ return $ bgroup "Equality"
25+ [ bench "Text" $ whnf (== T.init t `T.snoc` '\xfffd') t
26+ , bench "LazyText" $ whnf (== TL.init tl `TL.snoc` '\xfffd') tl
27+ , bench "ByteString" $ whnf (== B.init b `B.snoc` '\xfffd') b
28+ , bench "LazyByteString" $ whnf (== BL.init bl `BL.snoc` '\xfffd') bl
29+ , bench "String" $ whnf (== init l ++ "\xfffd") l
2930 ]
30−
31−-- | Frequency of most-appearing line (from the first 100 lines)
32−--
33−func :: (Eq a) => [a] -> Int
34−func ls = sum . map (\needle -> length . filter (== needle) $ ls) $ take 100 ls