Skip to content

Instantly share code, notes, and snippets.

@rsdy
Last active December 13, 2015 22:59
Show Gist options
  • Save rsdy/4988177 to your computer and use it in GitHub Desktop.
Save rsdy/4988177 to your computer and use it in GitHub Desktop.
haskell zipwith vs. map-zip
warming up
estimating clock resolution...
mean is 1.363583 us (640001 iterations)
found 3371 outliers among 639999 samples (0.5%)
2926 (0.5%) high severe
estimating cost of a clock call...
mean is 38.34752 ns (12 iterations)
found 1 outliers among 12 samples (8.3%)
1 (8.3%) low severe
benchmarking zip test/map
collecting 100 samples, 1 iterations each, in estimated 288.3517 s
mean: 56.73412 ms, lb 56.40832 ms, ub 57.35469 ms, ci 0.950
std dev: 2.227947 ms, lb 1.426694 ms, ub 4.053248 ms, ci 0.950
found 3 outliers among 100 samples (3.0%)
2 (2.0%) high severe
variance introduced by outliers: 36.537%
variance is moderately inflated by outliers
benchmarking zip test/zip
mean: 28.02551 ms, lb 27.90591 ms, ub 28.28736 ms, ci 0.950
std dev: 862.9143 us, lb 390.6073 us, ub 1.472298 ms, ci 0.950
found 16 outliers among 100 samples (16.0%)
3 (3.0%) high mild
13 (13.0%) high severe
variance introduced by outliers: 25.799%
variance is moderately inflated by outliers
import Criterion.Config
import Criterion.Main
import System.Random
main :: IO ()
main = newStdGen >>= defaultMainWith benchConfig (return ()) . benchmarks
randNumbers :: RandomGen g => g -> Int -> [Int]
randNumbers gen n = take n $ randoms gen
zipWithVals :: [Int] -> [Int] -> [Int]
zipWithVals xs xs' = zipWith (*) xs xs'
mapZipVals :: [Int] -> [Int] -> [Int]
mapZipVals xs xs' = map (\(x,y) -> x * y) $ zip xs xs'
benchmarks :: RandomGen g => g -> [Benchmark]
benchmarks gen =
let x1 = randNumbers gen n
x2 = randNumbers gen n
n = 1000000
in [
bgroup "zip test"
[
bench "map" $ nf (mapZipVals x1) x2
, bench "zip" $ nf (zipWithVals x1) x2
]
]
benchConfig :: Config
benchConfig = defaultConfig {
cfgPerformGC = ljust True
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment