Skip to content

Instantly share code, notes, and snippets.

@Davorak
Created April 25, 2013 02:33
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 Davorak/5457105 to your computer and use it in GitHub Desktop.
Save Davorak/5457105 to your computer and use it in GitHub Desktop.
Full code for stackoverflow question http://stackoverflow.com/q/16205086/128583.
ghc -fforce-recomp --make Main.hs -rtsopts -O2
{-# LANGUAGE MagicHash #-}
import GHC.Base
import Criterion
import Criterion.Main
myLength1 :: [a] -> Int
myLength1 [] = 0
myLength1 (x:xs) = 1 + myLength1 xs
myLength2 :: [a] -> Int
myLength2 lst = len lst 0
where
len :: [a] -> Int -> Int
len [] n = n
len (_:xs) n = len xs (n+1)
myLength3 :: [a] -> Int
myLength3 l = len l 0#
where
len :: [a] -> Int# -> Int
len [] a# = I# a#
len (_:xs) a# = len xs (a# +# 1#)
main = do
let getList () = [1..10^6] :: [Int]
defaultMain [
bench "Prelude Length" $ nf length (getList ())
, bench "myLength1" $ nf myLength1 (getList ())
, bench "myLength2" $ nf myLength2 (getList ())
, bench "myLength3" $ nf myLength3 (getList ())
]
$ ./Main +RTS -K200M -RTS
warming up
estimating clock resolution...
mean is 1.764828 us (320001 iterations)
found 4533 outliers among 319999 samples (1.4%)
2910 (0.9%) high severe
estimating cost of a clock call...
mean is 47.78672 ns (12 iterations)
benchmarking Prelude Length
collecting 100 samples, 1 iterations each, in estimated 11.64012 s
mean: 5.259745 ms, lb 5.223572 ms, ub 5.307336 ms, ci 0.950
std dev: 211.0929 us, lb 168.2398 us, ub 311.2242 us, ci 0.950
found 2 outliers among 100 samples (2.0%)
1 (1.0%) high severe
variance introduced by outliers: 37.525%
variance is moderately inflated by outliers
benchmarking myLength1
mean: 12.88293 ms, lb 12.76815 ms, ub 13.08463 ms, ci 0.950
std dev: 753.5147 us, lb 499.0208 us, ub 1.317084 ms, ci 0.950
found 3 outliers among 100 samples (3.0%)
3 (3.0%) high severe
variance introduced by outliers: 56.464%
variance is severely inflated by outliers
benchmarking myLength2
mean: 5.202620 ms, lb 5.166745 ms, ub 5.249734 ms, ci 0.950
std dev: 209.9635 us, lb 169.0609 us, ub 289.0847 us, ci 0.950
found 6 outliers among 100 samples (6.0%)
5 (5.0%) high mild
1 (1.0%) high severe
variance introduced by outliers: 37.541%
variance is moderately inflated by outliers
benchmarking myLength3
mean: 5.639372 ms, lb 5.432358 ms, ub 6.329498 ms, ci 0.950
std dev: 1.723670 ms, lb 557.8348 us, ub 3.835811 ms, ci 0.950
found 8 outliers among 100 samples (8.0%)
2 (2.0%) high mild
6 (6.0%) high severe
variance introduced by outliers: 97.839%
variance is severely inflated by outliers
$ ./Main +RTS -K200M -RTS
warming up
estimating clock resolution...
mean is 2.144997 us (320001 iterations)
found 2666 outliers among 319999 samples (0.8%)
2112 (0.7%) high severe
estimating cost of a clock call...
mean is 45.13869 ns (17 iterations)
found 1 outliers among 17 samples (5.9%)
1 (5.9%) high mild
benchmarking Prelude Length
collecting 100 samples, 1 iterations each, in estimated 13.21990 s
mean: 5.481868 ms, lb 5.442965 ms, ub 5.523074 ms, ci 0.950
std dev: 204.9174 us, lb 180.9730 us, ub 235.3237 us, ci 0.950
variance introduced by outliers: 33.630%
variance is moderately inflated by outliers
benchmarking myLength1
collecting 100 samples, 1 iterations each, in estimated 27.31569 s
mean: 202.1552 ms, lb 191.6589 ms, ub 214.4650 ms, ci 0.950
std dev: 58.10625 ms, lb 49.10650 ms, ub 73.54255 ms, ci 0.950
found 2 outliers among 100 samples (2.0%)
2 (2.0%) high mild
variance introduced by outliers: 97.818%
variance is severely inflated by outliers
benchmarking myLength2
collecting 100 samples, 1 iterations each, in estimated 32.15361 s
mean: 236.3042 ms, lb 225.4061 ms, ub 249.8233 ms, ci 0.950
std dev: 61.85220 ms, lb 50.97809 ms, ub 91.10013 ms, ci 0.950
found 1 outliers among 100 samples (1.0%)
variance introduced by outliers: 96.785%
variance is severely inflated by outliers
benchmarking myLength3
collecting 100 samples, 1 iterations each, in estimated 12.35778 s
mean: 5.363071 ms, lb 5.294631 ms, ub 5.577624 ms, ci 0.950
std dev: 563.9155 us, lb 211.5998 us, ub 1.243479 ms, ci 0.950
found 4 outliers among 100 samples (4.0%)
3 (3.0%) high severe
variance introduced by outliers: 81.050%
variance is severely inflated by outliers
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment