Skip to content

Instantly share code, notes, and snippets.

@tfausak
Created January 6, 2021 16:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tfausak/5e3eced55a79b13d91f49c92aca258fc to your computer and use it in GitHub Desktop.
Save tfausak/5e3eced55a79b13d91f49c92aca258fc to your computer and use it in GitHub Desktop.
{-
benchmarked show
time 18.81 ns (18.78 ns .. 18.85 ns)
1.000 R² (1.000 R² .. 1.000 R²)
mean 18.80 ns (18.79 ns .. 18.82 ns)
std dev 41.27 ps (27.30 ps .. 70.30 ps)
benchmarked printf
time 338.0 ns (334.5 ns .. 341.2 ns)
0.999 R² (0.999 R² .. 1.000 R²)
mean 336.2 ns (335.0 ns .. 337.7 ns)
std dev 4.443 ns (3.910 ns .. 5.148 ns)
benchmarked pack . show -- text
time 27.79 ns (27.78 ns .. 27.80 ns)
1.000 R² (1.000 R² .. 1.000 R²)
mean 27.80 ns (27.79 ns .. 27.81 ns)
std dev 34.10 ps (27.95 ps .. 42.86 ps)
benchmarked pack . show -- lazy text
time 88.07 ns (87.29 ns .. 89.00 ns)
1.000 R² (0.999 R² .. 1.000 R²)
mean 88.45 ns (88.15 ns .. 88.75 ns)
std dev 1.054 ns (924.9 ps .. 1.207 ns)
benchmarked toStrict . toLazyText . decimal
time 67.73 ns (67.62 ns .. 67.84 ns)
1.000 R² (1.000 R² .. 1.000 R²)
mean 67.83 ns (67.76 ns .. 67.92 ns)
std dev 277.4 ps (218.0 ps .. 339.6 ps)
benchmarked pack . show -- bytestring
time 35.70 ns (35.58 ns .. 35.82 ns)
1.000 R² (1.000 R² .. 1.000 R²)
mean 35.85 ns (35.78 ns .. 35.88 ns)
std dev 159.2 ps (114.6 ps .. 241.8 ps)
benchmarked pack . show -- lazy bytestring
time 39.39 ns (39.14 ns .. 39.58 ns)
1.000 R² (1.000 R² .. 1.000 R²)
mean 39.52 ns (39.44 ns .. 39.60 ns)
std dev 274.0 ps (185.2 ps .. 400.5 ps)
benchmarked toStrict . toLazyByteString . intDec
time 125.2 ns (124.9 ns .. 125.4 ns)
1.000 R² (1.000 R² .. 1.000 R²)
mean 125.3 ns (125.2 ns .. 125.4 ns)
std dev 373.7 ps (317.8 ps .. 439.6 ps)
benchmarked itoa
time 47.45 ns (47.31 ns .. 47.58 ns)
1.000 R² (1.000 R² .. 1.000 R²)
mean 47.30 ns (47.27 ns .. 47.34 ns)
std dev 116.6 ps (96.91 ps .. 166.0 ps)
-}
import qualified Data.ByteString.Builder as BB
import qualified Data.ByteString.Char8 as BS
import qualified Data.ByteString.Lazy.Char8 as BL
import qualified Data.Text as TS
import qualified Data.Text.Lazy as TL
import qualified Data.Text.Lazy.Builder as TB
import qualified Data.Text.Lazy.Builder.Int as TB
import qualified Gauge
import qualified Text.Printf as Printf
main = let x = 123 :: Int in Gauge.defaultMain
[ let f = show :: Int -> String
in Gauge.bench "show" $ Gauge.nf f x
, let f = Printf.printf "%d" :: Int -> String
in Gauge.bench "printf" $ Gauge.nf f x
, let f = TS.pack . show :: Int -> TS.Text
in Gauge.bench "pack . show -- text" $ Gauge.nf f x
, let f = TL.pack . show :: Int -> TL.Text
in Gauge.bench "pack . show -- lazy text" $ Gauge.nf f x
, let f = TL.toStrict . TB.toLazyText . TB.decimal :: Int -> TS.Text
in Gauge.bench "toStrict . toLazyText . decimal" $ Gauge.nf f x
, let f = BS.pack . show :: Int -> BS.ByteString
in Gauge.bench "pack . show -- bytestring" $ Gauge.nf f x
, let f = BL.pack . show :: Int -> BL.ByteString
in Gauge.bench "pack . show -- lazy bytestring" $ Gauge.nf f x
, let f = BL.toStrict . BB.toLazyByteString . BB.intDec :: Int -> BS.ByteString
in Gauge.bench "toStrict . toLazyByteString . intDec" $ Gauge.nf f x
, Gauge.bench "itoa" $ Gauge.nf itoa x
]
itoa :: Int -> String
itoa x = case x of
0 -> "0"
1 -> "1"
2 -> "2"
3 -> "3"
4 -> "4"
5 -> "5"
6 -> "6"
7 -> "7"
8 -> "8"
9 -> "9"
_ | x < 0 -> '-' : itoa x
| otherwise -> let (q, r) = quotRem x 10 in itoa q <> itoa r
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment