Created
December 15, 2020 08:29
-
-
Save kwannoel/1ea565114fbbd635e18a6a1c62ee70fc to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env stack | |
-- stack script --resolver lts-16.2 --package criterion --package relude --package text --package deepseq | |
{-# LANGUAGE DeriveGeneric #-} | |
{-# LANGUAGE NumericUnderscores #-} | |
{-# LANGUAGE OverloadedStrings #-} | |
import Control.DeepSeq (NFData) | |
import Control.Monad (replicateM_, void) | |
import Criterion.Main | |
import Criterion.Types | |
import Data.Foldable (foldl', foldr) | |
import Data.Text (Text) | |
import GHC.Generics (Generic) | |
import Relude.Extra.Enum (inverseMap) | |
data Sum5 = One | |
| Two | |
| Three | |
| Four | |
| Five | |
deriving (Eq, Show, Bounded, Enum, Generic) | |
instance NFData Sum5 | |
main :: IO () | |
main = defaultMainWith config | |
[ | |
bgroup "serialize" [ bench "inverseMap" $ nf (fmap $ inverseMap deserialize) ["One", "Two", "Three", "Four", "Five"] | |
, bench "inverseManual" $ nf (fmap inverseManual) ["One", "Two", "Three", "Four", "Five"] | |
] | |
, bgroup "serialize-num" [ bench "inverseMap" $ nf (fmap $ inverseMap deserializeInt) [1..5] | |
, bench "inverseManual" $ nf (fmap inverseManualInt) [1..5] | |
] | |
] | |
where | |
deserialize :: Sum5 -> Text | |
deserialize One = "One" | |
deserialize Two = "Two" | |
deserialize Three = "Three" | |
deserialize Four = "Four" | |
deserialize Five = "Five" | |
inverseManual :: Text -> Sum5 | |
inverseManual "One" = One | |
inverseManual "Two" = Two | |
inverseManual "Three" = Three | |
inverseManual "Four" = Four | |
inverseManual "Five" = Five | |
deserializeInt :: Sum5 -> Int | |
deserializeInt One = 1 | |
deserializeInt Two = 2 | |
deserializeInt Three = 3 | |
deserializeInt Four = 4 | |
deserializeInt Five = 5 | |
inverseManualInt :: Int -> Sum5 | |
inverseManualInt 1 = One | |
inverseManualInt 2 = Two | |
inverseManualInt 3 = Three | |
inverseManualInt 4 = Four | |
inverseManualInt 5 = Five | |
config :: Config | |
config = defaultConfig { resamples = 10_000 } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
benchmarking serialize/inverseMap | |
time 1.632 μs (1.619 μs .. 1.649 μs) | |
0.999 R² (0.999 R² .. 1.000 R²) | |
mean 1.627 μs (1.616 μs .. 1.643 μs) | |
std dev 43.40 ns (33.37 ns .. 57.62 ns) | |
variance introduced by outliers: 34% (moderately inflated) | |
benchmarking serialize/inverseManual | |
time 7.659 μs (7.611 μs .. 7.725 μs) | |
0.999 R² (0.999 R² .. 1.000 R²) | |
mean 7.662 μs (7.609 μs .. 7.739 μs) | |
std dev 210.2 ns (153.7 ns .. 303.5 ns) | |
variance introduced by outliers: 32% (moderately inflated) | |
benchmarking serialize-num/inverseMap | |
time 1.502 μs (1.488 μs .. 1.520 μs) | |
0.999 R² (0.998 R² .. 1.000 R²) | |
mean 1.502 μs (1.491 μs .. 1.518 μs) | |
std dev 43.36 ns (32.22 ns .. 59.94 ns) | |
variance introduced by outliers: 38% (moderately inflated) | |
benchmarking serialize-num/inverseManual | |
time 2.002 μs (1.972 μs .. 2.035 μs) | |
0.999 R² (0.998 R² .. 0.999 R²) | |
mean 2.001 μs (1.987 μs .. 2.018 μs) | |
std dev 52.40 ns (42.56 ns .. 65.95 ns) | |
variance introduced by outliers: 33% (moderately inflated) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment