[Haskell-cafe] Why does not zipWith' exist

Daniel Fischer daniel.is.fischer at googlemail.com
Fri Feb 1 13:22:50 CET 2013


On Friday 01 February 2013, 13:06:09, Daniel Fischer wrote:
> 
> zipWith' would [I haven't tested, but I'm rather confident] make a
> difference if you benchmarked
> 
>     bench "name" (whnf (fibs !!) 100000)
> 
> etc.

Well, it took a little bit of persuasion to let GHC not cache the list(s), but 
with


fibs :: Int -> Integer
fibs k = igo i !! k
  where
    i | k < 1000000 = 1
      | otherwise   = 2
    igo :: Integer -> [Integer]
    igo i = let go = 0 : i : zipWith (+) go (tail go) in go

etc., benchmarking

main :: IO ()
main = defaultMain $ [
    bench "fibs " (whnf fibs 20000)
  , bench "fibsP" (whnf fibsP 20000)
  , bench "fibs'" (whnf fibs' 20000)
  ]

shows a clear difference:

benchmarking fibs 
mean: 14.50178 ms, lb 14.27410 ms, ub 14.78909 ms, ci 0.950
benchmarking fibsP
mean: 13.69060 ms, lb 13.59516 ms, ub 13.81583 ms, ci 0.950
benchmarking fibs'
mean: 3.155886 ms, lb 3.137776 ms, ub 3.177367 ms, ci 0.950




More information about the Haskell-Cafe mailing list