Skip to main content
3 of 5
deleted 1 character in body; added 1 character in body
Carcigenicate
  • 2.7k
  • 3
  • 26
  • 40

Why does Haskell's built in max function run faster then mine?

I noticed that for some reason, Haskell's built in max function (which returns the greatest of two numbers) runs much faster then the one I wrote, even though they are essentially identical.

From this site: http://www.haskell.org/onlinereport/standard-prelude.html, I found that the standard max function is defined as:

    max x y 
     | x <= y    =  y
     | otherwise =  x

which is capable of executing

foldr max 0 [0..10000000]

in 7.6 seconds (my laptop is in super power saving mode)

I wrote the exact same function though, and ran it, and

foldr myMax 0 [0..10000000]

took an average of 23.74 seconds

The two functions look identical, except that the builtin max doesnt seem to have a type signature (unless its hidden somewhere.)

Does anyone know what might be going on here? I seriously doubt that a built in function will run more then three times faster then an identical, user defined one. To me that would be very strange.

(When I say that they're identical, I mean literally clones of each other. Just to test, I C&P'd it right out of the Prelude, and it's still significantly slower.)

Carcigenicate
  • 2.7k
  • 3
  • 26
  • 40