git.iko.soy/typeable/bytestring-conversion
on master help sign in
62 lines 2.2 kB history raw
1-- This Source Code Form is subject to the terms of the Mozilla Public
2-- License, v. 2.0. If a copy of the MPL was not distributed with this
3-- file, You can obtain one at http://mozilla.org/MPL/2.0/.
5{-# LANGUAGE OverloadedStrings #-}
7module Main (main) where
9import Criterion
10import Criterion.Main
11import Data.ByteString.Conversion
12import Data.Int
13import Data.Monoid
14import Data.Text.Encoding (encodeUtf8)
15import Data.Word
17import qualified Data.ByteString.Char8 as B
18import qualified Data.Text as T
20main :: IO ()
21main = defaultMain
22 [ bgroup "bytestring-to : int"
23 [ bench "1" $ whnf toByteString (1 :: Int8)
24 , bench "2" $ whnf toByteString (19 :: Int16)
25 , bench "4" $ whnf toByteString (1234 :: Int32)
26 , bench "8" $ whnf toByteString (12345678 :: Int64)
27 , bench "16" $ whnf toByteString (1234567812345678 :: Integer)
28 ]
29 , bgroup "pack . show : int"
30 [ bench "1" $ whnf (B.pack . show) (1 :: Int8)
31 , bench "2" $ whnf (B.pack . show) (19 :: Int16)
32 , bench "4" $ whnf (B.pack . show) (1234 :: Int32)
33 , bench "8" $ whnf (B.pack . show) (12345678 :: Int64)
34 , bench "16" $ whnf (B.pack . show) (1234567812345678 :: Integer)
35 ]
36 , bgroup "bytestring-to : word"
37 [ bench "1" $ whnf toByteString (1 :: Word8)
38 , bench "2" $ whnf toByteString (19 :: Word16)
39 , bench "4" $ whnf toByteString (1234 :: Word32)
40 , bench "8" $ whnf toByteString (12345678 :: Word64)
41 ]
42 , bgroup "pack . show : word"
43 [ bench "1" $ whnf (B.pack . show) (1 :: Word8)
44 , bench "2" $ whnf (B.pack . show) (19 :: Word16)
45 , bench "4" $ whnf (B.pack . show) (1234 :: Word32)
46 , bench "8" $ whnf (B.pack . show) (12345678 :: Word64)
47 ]
48 , bgroup "bytestring-to : text"
49 [ bench "26" $ whnf toByteString txt26
50 , bench "78" $ whnf toByteString txt78
51 ]
52 , bgroup "encodeUtf8 : txt"
53 [ bench "26" $ whnf encodeUtf8 txt26
54 , bench "78" $ whnf encodeUtf8 txt78
55 ]
56 ]
58txt26 :: T.Text
59txt26 = T.pack $ ['a' .. 'z']
61txt78 :: T.Text
62txt78 = txt26 <> txt26 <> txt26