on bebb340 help sign in
33 lines 901 B history raw
1-- | UTF-8 encode a text
2--
3-- Tested in this benchmark:
4--
5-- * Replicating a string a number of times
6--
7-- * UTF-8 encoding it
8--
9module Benchmarks.EncodeUtf8
10 ( benchmark
11 ) where
13import Criterion (Benchmark, bgroup, bench, whnf)
14import qualified Data.ByteString as B
15import qualified Data.ByteString.Lazy as BL
16import qualified Data.Text as T
17import qualified Data.Text.Encoding as T
18import qualified Data.Text.Lazy as TL
19import qualified Data.Text.Lazy.Encoding as TL
21benchmark :: String -> IO Benchmark
22benchmark string = do
23 return $ bgroup "EncodeUtf8"
24 [ bench "Text" $ whnf (B.length . T.encodeUtf8) text
25 , bench "LazyText" $ whnf (BL.length . TL.encodeUtf8) lazyText
26 ]
27 where
28 -- The string in different formats
29 text = T.replicate k $ T.pack string
30 lazyText = TL.replicate (fromIntegral k) $ TL.pack string
32 -- Amount
33 k = 100000