Commits

Bryan O'Sullivan  committed 4ab5d2d

Replace the equality test with something less noisy.

  • Participants
  • Parent commits 569aa55

Comments (0)

Files changed (1)

File tests/benchmarks/src/Data/Text/Benchmarks/Equality.hs

--- | Take the first 100 lines from the file, and count the number of lines equal
--- to those lines
+-- | Compare a string with a copy of itself that is identical except
+-- for the last character.
 --
 module Data.Text.Benchmarks.Equality
-    ( benchmark
+    (
+      benchmark
     ) where
 
-import Control.Exception (evaluate)
-import Criterion (Benchmark, bgroup, bench)
+import Criterion (Benchmark, bgroup, bench, whnf)
 import qualified Data.ByteString.Char8 as B
 import qualified Data.ByteString.Lazy.Char8 as BL
 import qualified Data.Text as T
 import qualified Data.Text.Lazy.Encoding as TL
 
 benchmark :: FilePath -> IO Benchmark
-benchmark fp = return $ bgroup "Equality"
-    [ bench "Text" $
-        B.readFile fp >>= evaluate . func . T.lines . T.decodeUtf8
-    , bench "LazyText" $
-        BL.readFile fp >>= evaluate . func . TL.lines . TL.decodeUtf8
-    , bench "ByteString" $
-        B.readFile fp >>= evaluate . func . B.lines
-    , bench "LazyByteString" $
-        BL.readFile fp >>= evaluate . func . BL.lines
-    , bench "String" $
-        readFile fp >>= evaluate . func . lines
+benchmark fp = do
+  b <- B.readFile fp
+  bl <- BL.readFile fp
+  l <- readFile fp
+  let t  = T.decodeUtf8 b
+      tl = TL.decodeUtf8 bl
+  return $ bgroup "Equality"
+    [ bench "Text" $ whnf (== T.init t `T.snoc` '\xfffd') t
+    , bench "LazyText" $ whnf (== TL.init tl `TL.snoc` '\xfffd') tl
+    , bench "ByteString" $ whnf (== B.init b `B.snoc` '\xfffd') b
+    , bench "LazyByteString" $ whnf (== BL.init bl `BL.snoc` '\xfffd') bl
+    , bench "String" $ whnf (== init l ++ "\xfffd") l
     ]
-
--- | Frequency of most-appearing line (from the first 100 lines)
---
-func :: (Eq a) => [a] -> Int
-func ls = sum . map (\needle -> length . filter (== needle) $ ls) $ take 100 ls