Skip to content

Inconsistent loop performance

Here are two small benchmarks:

import Criterion.Main

foo :: Int -> Int -> Int
foo n k | n <= 0    = k
        | otherwise = foo (n-1) (k+1)

bar :: Int -> Int -> Int
bar n k | n == 0    = k
        | otherwise = bar (n-1) (k+1)

main :: IO ()
main = defaultMain [ bench "foo" $ nf (uncurry foo) (20000000,0)
                   , bench "bar" $ nf (uncurry bar) (20000000,0)
                   ]

On my laptop, I consistently get a mean running time of about 36ms for foo and about 45ms for bar. This is a rather big difference. The Core looks about the same and going via C gives me about 42ms for both loops so this is certainly a code generator problem.

Trac metadata
Trac field Value
Version 6.13
Type Bug
TypeOfFailure OtherFailure
Priority normal
Resolution Unresolved
Component Compiler
Test case
Differential revisions
BlockedBy
Related
Blocking
CC
Operating system
Architecture
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information