1. Bryan O'Sullivan
  2. text

Commits

Jasper Van der Jeugt  committed 11d6151

Sort benchmark, reloaded

  • Participants
  • Parent commits 9b34570
  • Branches default

Comments (0)

Files changed (3)

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

View file
  • Ignore whitespace
 import qualified Data.Text.Benchmarks.Pure as Pure
 import qualified Data.Text.Benchmarks.ReadNumbers as ReadNumbers
 import qualified Data.Text.Benchmarks.Replace as Replace
-import qualified Data.Text.Benchmarks.Sort as Sort
 import qualified Data.Text.Benchmarks.StripBrackets as StripBrackets
 import qualified Data.Text.Benchmarks.WordCount as WordCount
 
 import qualified Data.Text.Benchmarks.Programs.Cut as Programs.Cut
+import qualified Data.Text.Benchmarks.Programs.Sort as Programs.Sort
 import qualified Data.Text.Benchmarks.Programs.Throughput as Programs.Throughput
 
 main :: IO ()
         , Pure.benchmark (tf "japanese.txt")
         , ReadNumbers.benchmark (tf "numbers.txt")
         , Replace.benchmark (tf "russian.txt") sink "принимая" "своем"
-        , Sort.benchmark (tf "russian.txt") sink
         , StripBrackets.benchmark (tf "russian.txt") sink
         , WordCount.benchmark (tf "russian.txt")
         ]
     -- Program-like benchmarks
     ps <- bgroup "Programs" `fmap` sequence
         [ Programs.Cut.benchmark (tf "russian.txt") sink 20 40
+        , Programs.Sort.benchmark (tf "russian.txt") sink
         , Programs.Throughput.benchmark (tf "russian.txt") sink
         ]
 

File tests/benchmarks/src/Data/Text/Benchmarks/Programs/Sort.hs

View file
  • Ignore whitespace
+-- | This benchmark sorts the lines of a file, like the @sort@ unix utility.
+--
+{-# LANGUAGE OverloadedStrings #-}
+module Data.Text.Benchmarks.Programs.Sort
+    ( benchmark
+    ) where
+
+import Criterion (Benchmark, bgroup, bench)
+import Data.Monoid (mconcat)
+import System.IO (Handle, hPutStr)
+import qualified Data.ByteString as B
+import qualified Data.ByteString.Lazy as BL
+import qualified Data.ByteString.Lazy.Char8 as BLC
+import qualified Data.ByteString.Char8 as BC
+import qualified Data.List as L
+import qualified Data.Text as T
+import qualified Data.Text.IO as T
+import qualified Data.Text.Lazy as TL
+import qualified Data.Text.Lazy.IO as TL
+import qualified Data.Text.Lazy.Builder as TLB
+
+benchmark :: FilePath -> Handle -> IO Benchmark
+benchmark i o = return $ bgroup "Sort"
+    [ bench "String" $ readFile i >>= hPutStr o . string
+    , bench "ByteString" $ B.readFile i >>= B.hPutStr o . byteString
+    , bench "LazyByteString" $ BL.readFile i >>= BL.hPutStr o . lazyByteString
+    , bench "Text" $ T.readFile i >>= T.hPutStr o . text
+    , bench "LazyText" $ TL.readFile i >>= TL.hPutStr o . lazyText
+    , bench "TextBuilder" $ T.readFile i >>= TL.hPutStr o . textBuilder
+    ]
+
+string :: String -> String
+string = unlines . L.sort . lines
+
+byteString :: B.ByteString -> B.ByteString
+byteString = BC.unlines . L.sort . BC.lines
+
+lazyByteString :: BL.ByteString -> BL.ByteString
+lazyByteString = BLC.unlines . L.sort . BLC.lines
+
+text :: T.Text -> T.Text
+text = T.unlines . L.sort . T.lines
+
+lazyText :: TL.Text -> TL.Text
+lazyText = TL.unlines . L.sort . TL.lines
+
+-- | Text variant using a builder monoid for the final concatenation
+--
+textBuilder :: T.Text -> TL.Text
+textBuilder = TLB.toLazyText . mconcat . L.intersperse (TLB.singleton '\n') .
+    map TLB.fromText . L.sort . T.lines

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

  • Ignore whitespace
--- | Implements the unix @sort@ program
---
-{-# LANGUAGE OverloadedStrings #-}
-module Data.Text.Benchmarks.Sort
-    ( benchmark
-    ) where
-
-import Criterion (Benchmark, bench)
-import Data.Monoid (mconcat)
-import System.IO (Handle)
-import qualified Data.ByteString as B
-import qualified Data.ByteString.Lazy as BL
-import qualified Data.List as L
-import qualified Data.Text as T
-import qualified Data.Text.Encoding as T
-import qualified Data.Text.Lazy as TL
-import qualified Data.Text.Lazy.Builder as TLB
-import qualified Data.Text.Lazy.Encoding as TL
-
-benchmark :: FilePath -> Handle -> IO Benchmark
-benchmark fp sink = return $ bench "Sort" $ do
-    t <- T.decodeUtf8 `fmap` B.readFile fp
-    BL.hPutStr sink $ TL.encodeUtf8 $ sort t
-
-sort :: T.Text -> TL.Text
-sort = TLB.toLazyText . mconcat . L.intersperse (TLB.fromText "\n") .
-    map TLB.fromText . L.sort . T.lines