Skip to content

Instantly share code, notes, and snippets.

@kana-sama
Created June 28, 2021 21:24
Show Gist options
  • Save kana-sama/725e3c3fcb676c9e8311778da0824181 to your computer and use it in GitHub Desktop.
Save kana-sama/725e3c3fcb676c9e8311778da0824181 to your computer and use it in GitHub Desktop.
% hpack; cabal run --ghc-options "-O2"
benchmarking strict
time 115.7 μs (114.9 μs .. 116.5 μs)
0.999 R² (0.998 R² .. 0.999 R²)
mean 118.8 μs (116.9 μs .. 121.0 μs)
std dev 6.537 μs (5.279 μs .. 7.705 μs)
variance introduced by outliers: 56% (severely inflated)
benchmarking lazy
time 529.3 μs (523.9 μs .. 535.1 μs)
0.999 R² (0.999 R² .. 1.000 R²)
mean 522.7 μs (517.5 μs .. 527.4 μs)
std dev 16.70 μs (13.86 μs .. 21.91 μs)
variance introduced by outliers: 24% (moderately inflated)
benchmarking naive
time 96.55 μs (94.11 μs .. 98.95 μs)
0.996 R² (0.995 R² .. 0.998 R²)
mean 95.40 μs (94.04 μs .. 96.69 μs)
std dev 4.324 μs (3.588 μs .. 5.266 μs)
variance introduced by outliers: 48% (moderately inflated)
% hpack; cabal run
benchmarking strict
time 112.0 μs (109.6 μs .. 114.5 μs)
0.995 R² (0.991 R² .. 0.998 R²)
mean 116.4 μs (114.1 μs .. 121.6 μs)
std dev 10.78 μs (4.249 μs .. 19.96 μs)
variance introduced by outliers: 79% (severely inflated)
benchmarking lazy
time 496.3 μs (484.7 μs .. 507.0 μs)
0.997 R² (0.996 R² .. 0.998 R²)
mean 493.1 μs (486.7 μs .. 502.3 μs)
std dev 25.10 μs (18.34 μs .. 43.04 μs)
variance introduced by outliers: 45% (moderately inflated)
benchmarking naive
time 84.27 μs (83.21 μs .. 85.31 μs)
0.999 R² (0.999 R² .. 0.999 R²)
mean 84.05 μs (83.47 μs .. 85.14 μs)
std dev 2.629 μs (1.641 μs .. 4.513 μs)
variance introduced by outliers: 30% (moderately inflated)
import Criterion.Main
data X = X !Int ![Int]
strict_acc :: [Int] -> [Int]
strict_acc [] = []
strict_acc xs =
let X m ys = replace_with_min m xs
in ys
where
replace_with_min :: Int -> [Int] -> X
replace_with_min m [x] = X x [m]
replace_with_min m (x : xs) =
let X m' xs' = replace_with_min m xs
in X (min x m') (m : xs')
lazy_acc :: [Int] -> [Int]
lazy_acc xs =
let (m, ys) = replace_with_min m xs
in ys
where
replace_with_min :: Int -> [Int] -> (Int, [Int])
replace_with_min m [] = (m, [])
replace_with_min m [x] = (x, [m])
replace_with_min m (x : xs) =
let (m', xs') = replace_with_min m xs
in (min x m', m : xs')
naive :: [Int] -> [Int]
naive xs = replicate (length xs) (minimum xs)
main =
defaultMain
[ bench "strict" $ nf strict_acc [10000, 9999 .. 1],
bench "lazy" $ nf lazy_acc [10000, 9999 .. 1],
bench "naive" $ nf naive [10000, 9999 .. 1]
]
name: haskell-playground
dependencies:
- base >= 4.12 && < 5
- criterion
executables:
haskell-playground-exe:
main: Main.hs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment