File: Throughput.hs

package info (click to toggle)
haskell-text 1.2.0.6-1~bpo8%2B1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-backports
  • size: 900 kB
  • sloc: haskell: 9,299; ansic: 238; python: 87; ruby: 84; sh: 49; makefile: 29
file content (41 lines) | stat: -rw-r--r-- 1,610 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
-- | This benchmark simply reads and writes a file using the various string
-- libraries. The point of it is that we can make better estimations on how
-- much time the other benchmarks spend doing IO.
--
-- Note that we expect ByteStrings to be a whole lot faster, since they do not
-- do any actual encoding/decoding here, while String and Text do have UTF-8
-- encoding/decoding.
--
-- Tested in this benchmark:
--
-- * Reading the file
--
-- * Replacing text between HTML tags (<>) with whitespace
--
-- * Writing back to a handle
--
module Benchmarks.Programs.Throughput
    ( benchmark
    ) where

import Criterion (Benchmark, bgroup, bench, whnfIO)
import System.IO (Handle, hPutStr)
import qualified Data.ByteString as B
import qualified Data.ByteString.Lazy as BL
import qualified Data.Text.Encoding as T
import qualified Data.Text.IO as T
import qualified Data.Text.Lazy.Encoding as TL
import qualified Data.Text.Lazy.IO as TL

benchmark :: FilePath -> Handle -> IO Benchmark
benchmark fp sink = return $ bgroup "Throughput"
    [ bench "String" $ whnfIO $ readFile fp >>= hPutStr sink
    , bench "ByteString" $ whnfIO $ B.readFile fp >>= B.hPutStr sink
    , bench "LazyByteString" $ whnfIO $ BL.readFile fp >>= BL.hPutStr sink
    , bench "Text" $ whnfIO $ T.readFile fp >>= T.hPutStr sink
    , bench "LazyText" $ whnfIO $ TL.readFile fp >>= TL.hPutStr sink
    , bench "TextByteString" $ whnfIO $
        B.readFile fp >>= B.hPutStr sink . T.encodeUtf8 .  T.decodeUtf8
    , bench "LazyTextByteString" $ whnfIO $
        BL.readFile fp >>= BL.hPutStr sink . TL.encodeUtf8 . TL.decodeUtf8
    ]