Source

text / tests / benchmarks / src / Data / Text / Benchmarks / EncodeUtf8.hs

The default branch has multiple heads

-- | UTF-8 encode a text
--
-- Tested in this benchmark:
--
-- * Replicating a string a number of times
--
-- * UTF-8 encoding it
--
module Data.Text.Benchmarks.EncodeUtf8
    ( benchmark
    ) where

import Criterion (Benchmark, bgroup, bench, whnf)
import qualified Data.ByteString as B
import qualified Data.ByteString.Lazy as BL
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.Encoding as TL

benchmark :: String -> IO Benchmark
benchmark string = do
    return $ bgroup "EncodeUtf8"
        [ bench "Text"     $ whnf (B.length . T.encodeUtf8)   text
        , bench "LazyText" $ whnf (BL.length . TL.encodeUtf8) lazyText
        ]
  where
    -- The string in different formats
    text = T.replicate k $ T.pack string
    lazyText = TL.replicate (fromIntegral k) $ TL.pack string

    -- Amount
    k = 100000
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.