Skip to content

Commit

Permalink
Update benchmark
Browse files Browse the repository at this point in the history
Simplify and add a benchmark for Int.
  • Loading branch information
wismill committed Sep 1, 2023
1 parent f23599d commit 0773d3e
Showing 1 changed file with 20 additions and 11 deletions.
31 changes: 20 additions & 11 deletions bench/BenchHexadecimal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -21,42 +21,51 @@ import qualified Text.Builder
word :: Word
word = 123456789123456789

benchLazyBuilder Int T.Text
benchLazyBuilder Word T.Text
benchLazyBuilder = toStrict . toLazyText . go mempty
where
go !acc 0 = acc
go !acc n = let i = fromIntegral n * word in go (hexadecimal i <> (acc <> hexadecimal i)) (n - 1)
go !acc n = let i = n * word in go (hexadecimal i <> (acc <> hexadecimal i)) (n - 1)

benchLazyBuilderBS Int B.ByteString
benchLazyBuilderBS Word B.ByteString
benchLazyBuilderBS = B.toStrict . B.toLazyByteString . go mempty
where
go !acc 0 = acc
go !acc n = go (B.wordHex (fromIntegral n) <> (acc <> B.wordHex (fromIntegral n))) (n - 1)
go !acc n = go (B.wordHex n <> (acc <> B.wordHex n)) (n - 1)

#ifdef MIN_VERSION_text_builder
benchStrictBuilder Int T.Text
benchStrictBuilder Word T.Text
benchStrictBuilder = Text.Builder.run . go mempty
where
go !acc 0 = acc
go !acc n = let i = fromIntegral n * word in go (Text.Builder.hexadecimal i <> (acc <> Text.Builder.hexadecimal i)) (n - 1)
go !acc n = let i = n * word in go (Text.Builder.hexadecimal i <> (acc <> Text.Builder.hexadecimal i)) (n - 1)
#endif

benchLinearBuilder Int T.Text
benchLinearBuilder m = runBuffer (\b go b m)
benchLinearBuilderWord Word T.Text
benchLinearBuilderWord m = runBuffer (\b go b m)
where
go Buffer Word Buffer
go !acc 0 = acc
go !acc n = let i = n * word in go (i &<| (acc |>& i)) (n - 1)

benchLinearBuilderInt Word T.Text
benchLinearBuilderInt m = runBuffer (\b go b (fromIntegral m))
where
go Buffer Int Buffer
go !acc 0 = acc
go !acc n = let i = fromIntegral n * word in go (i &<| (acc |>& i)) (n - 1)
go !acc n = let i = n * fromIntegral word in go (i &<| (acc |>& i)) (n - 1)

benchHexadecimal Benchmark
benchHexadecimal = bgroup "Hexadecimal" $ map mkGroup [1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6]

mkGroup :: Int Benchmark
mkGroup :: Word Benchmark
mkGroup n = bgroup (show n)
[ bench "Data.Text.Lazy.Builder" $ nf benchLazyBuilder n
, bench "Data.ByteString.Builder" $ nf benchLazyBuilderBS n
#ifdef MIN_VERSION_text_builder
, bench "Text.Builder" $ nf benchStrictBuilder n
#endif
, bench "Data.Text.Builder.Linear" $ nf benchLinearBuilder n
, bench "Data.Text.Builder.Linear (Word)" $ nf benchLinearBuilderWord n
, bench "Data.Text.Builder.Linear (Int)" $ nf benchLinearBuilderInt n
]
where

0 comments on commit 0773d3e

Please sign in to comment.