Skip to content

Instantly share code, notes, and snippets.

@brandonchinn178
Created May 16, 2026 05:44
Show Gist options
  • Select an option

  • Save brandonchinn178/e65f52c7353983bfd243ce4c51789dd0 to your computer and use it in GitHub Desktop.

Select an option

Save brandonchinn178/e65f52c7353983bfd243ce4c51789dd0 to your computer and use it in GitHub Desktop.
{-# LANGUAGE OverloadedStrings #-}
module Main where
import Criterion.Main
import Data.Text.Lazy qualified as TL
import Data.Text.Lazy.Builder qualified as TL
import Data.Text.Lazy.Builder.Int qualified as TL
import Data.String (fromString)
{-# INLINE viaShow #-}
viaShow :: Int -> TL.Builder
viaShow x = fromString (show x)
{-# INLINE viaDecimal #-}
viaDecimal :: Int -> TL.Builder
viaDecimal = TL.decimal
smallVal :: Int
smallVal = 42
medVal :: Int
medVal = 123456
largeVal :: Int
largeVal = maxBound
negVal :: Int
negVal = -98765
main :: IO ()
main = defaultMain
[ bgroup "small (42)"
[ bench "fromString . show" $ nf (TL.toLazyText . viaShow) smallVal
, bench "decimal" $ nf (TL.toLazyText . viaDecimal) smallVal
]
, bgroup "medium (123456)"
[ bench "fromString . show" $ nf (TL.toLazyText . viaShow) medVal
, bench "decimal" $ nf (TL.toLazyText . viaDecimal) medVal
]
, bgroup "large (maxBound)"
[ bench "fromString . show" $ nf (TL.toLazyText . viaShow) largeVal
, bench "decimal" $ nf (TL.toLazyText . viaDecimal) largeVal
]
, bgroup "negative (-98765)"
[ bench "fromString . show" $ nf (TL.toLazyText . viaShow) negVal
, bench "decimal" $ nf (TL.toLazyText . viaDecimal) negVal
]
, bgroup "composed (\"val=\" <> n)"
[ bench "fromString . show / small" $ nf (\x -> TL.toLazyText ("val=" <> viaShow x)) smallVal
, bench "decimal / small" $ nf (\x -> TL.toLazyText ("val=" <> viaDecimal x)) smallVal
, bench "fromString . show / large" $ nf (\x -> TL.toLazyText ("val=" <> viaShow x)) largeVal
, bench "decimal / large" $ nf (\x -> TL.toLazyText ("val=" <> viaDecimal x)) largeVal
]
]
benchmarking small (42)/fromString . show
time 24.22 ns (24.18 ns .. 24.28 ns)
1.000 R² (1.000 R² .. 1.000 R²)
mean 24.29 ns (24.22 ns .. 24.44 ns)
std dev 329.1 ps (190.8 ps .. 527.2 ps)
variance introduced by outliers: 16% (moderately inflated)
benchmarking small (42)/decimal
time 20.91 ns (20.47 ns .. 21.68 ns)
0.993 R² (0.983 R² .. 1.000 R²)
mean 20.76 ns (20.51 ns .. 21.48 ns)
std dev 1.327 ns (383.1 ps .. 2.627 ns)
variance introduced by outliers: 82% (severely inflated)
benchmarking medium (123456)/fromString . show
time 42.32 ns (40.52 ns .. 44.38 ns)
0.989 R² (0.984 R² .. 0.997 R²)
mean 40.98 ns (40.20 ns .. 42.27 ns)
std dev 3.363 ns (2.188 ns .. 4.671 ns)
variance introduced by outliers: 88% (severely inflated)
benchmarking medium (123456)/decimal
time 24.75 ns (23.86 ns .. 25.96 ns)
0.987 R² (0.972 R² .. 0.997 R²)
mean 24.48 ns (23.93 ns .. 25.76 ns)
std dev 2.534 ns (1.392 ns .. 4.822 ns)
variance introduced by outliers: 92% (severely inflated)
benchmarking large (maxBound)/fromString . show
time 121.8 ns (121.3 ns .. 122.6 ns)
1.000 R² (0.999 R² .. 1.000 R²)
mean 121.5 ns (121.2 ns .. 122.1 ns)
std dev 1.396 ns (668.0 ps .. 2.565 ns)
variance introduced by outliers: 11% (moderately inflated)
benchmarking large (maxBound)/decimal
time 35.04 ns (34.75 ns .. 35.40 ns)
0.999 R² (0.999 R² .. 1.000 R²)
mean 35.04 ns (34.80 ns .. 35.44 ns)
std dev 1.023 ns (696.5 ps .. 1.582 ns)
variance introduced by outliers: 47% (moderately inflated)
benchmarking negative (-98765)/fromString . show
time 43.97 ns (42.93 ns .. 45.27 ns)
0.997 R² (0.995 R² .. 0.999 R²)
mean 42.83 ns (42.37 ns .. 43.50 ns)
std dev 1.814 ns (1.229 ns .. 2.702 ns)
variance introduced by outliers: 65% (severely inflated)
benchmarking negative (-98765)/decimal
time 24.60 ns (23.96 ns .. 25.51 ns)
0.994 R² (0.989 R² .. 1.000 R²)
mean 24.24 ns (23.92 ns .. 24.98 ns)
std dev 1.467 ns (632.3 ps .. 2.435 ns)
variance introduced by outliers: 80% (severely inflated)
benchmarking composed ("val=" <> n)/fromString . show / small
time 43.56 ns (43.49 ns .. 43.65 ns)
1.000 R² (1.000 R² .. 1.000 R²)
mean 43.59 ns (43.52 ns .. 43.75 ns)
std dev 333.3 ps (139.6 ps .. 619.6 ps)
benchmarking composed ("val=" <> n)/decimal / small
time 45.99 ns (45.78 ns .. 46.20 ns)
1.000 R² (1.000 R² .. 1.000 R²)
mean 45.81 ns (45.71 ns .. 45.93 ns)
std dev 363.4 ps (300.2 ps .. 428.5 ps)
benchmarking composed ("val=" <> n)/fromString . show / large
time 142.7 ns (141.8 ns .. 143.9 ns)
1.000 R² (1.000 R² .. 1.000 R²)
mean 142.2 ns (141.9 ns .. 142.7 ns)
std dev 1.294 ns (826.5 ps .. 2.127 ns)
benchmarking composed ("val=" <> n)/decimal / large
time 61.66 ns (61.36 ns .. 62.04 ns)
1.000 R² (1.000 R² .. 1.000 R²)
mean 61.63 ns (61.44 ns .. 61.97 ns)
std dev 876.7 ps (520.0 ps .. 1.334 ns)
variance introduced by outliers: 16% (moderately inflated)
Benchmark builder-bench: FINISH
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment