Skip to main content

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

I noticed that for some reason, Haskell's built in max function (which returns the greatest of two numbers) runs much faster thenthan 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 builtinbuilt-in max doesntdoesn't 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 thenthan three times faster thenthan 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.)

Edit: I thought about it more, and I think it might have something to do with the included functions being pre-compiled, where-as my functions are being interpreted via GHCI (Which would make sense then). I'll leave this up in case someone has a better answer, but I suspect this to be the cause.

(One thing I realized that I don't understand though is why GHCI says thatsthat's it compiled my code after an edit, but then goes on the say that's it's interpreting it. You don't interpret compiled code do you?)

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.)

Edit: I thought about it more, and I think it might have something to do with the included functions being pre-compiled, where-as my functions are being interpreted via GHCI (Which would make sense then). I'll leave this up in case someone has a better answer, but I suspect this to be the cause.

(One thing I realized that I don't understand though is why GHCI says thats it compiled my code after an edit, but then goes on the say that's it's interpreting it. You don't interpret compiled code do you?)

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

I noticed that for some reason, Haskell's built in max function (which returns the greatest of two numbers) runs much faster than 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 built-in max doesn't 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 than three times faster than 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.)

Edit: I thought about it more, and I think it might have something to do with the included functions being pre-compiled, where-as my functions are being interpreted via GHCI (Which would make sense then). I'll leave this up in case someone has a better answer, but I suspect this to be the cause.

(One thing I realized that I don't understand though is why GHCI says that's it compiled my code after an edit, but then goes on the say that's it's interpreting it. You don't interpret compiled code do you?)

Tweeted twitter.com/#!/StackProgrammer/status/486335039974952960
added 507 characters in body
Source Link
Carcigenicate
  • 2.7k
  • 3
  • 26
  • 40

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.)

Edit: I thought about it more, and I think it might have something to do with the included functions being pre-compiled, where-as my functions are being interpreted via GHCI (Which would make sense then). I'll leave this up in case someone has a better answer, but I suspect this to be the cause.

(One thing I realized that I don't understand though is why GHCI says thats it compiled my code after an edit, but then goes on the say that's it's interpreting it. You don't interpret compiled code do you?)

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.)

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.)

Edit: I thought about it more, and I think it might have something to do with the included functions being pre-compiled, where-as my functions are being interpreted via GHCI (Which would make sense then). I'll leave this up in case someone has a better answer, but I suspect this to be the cause.

(One thing I realized that I don't understand though is why GHCI says thats it compiled my code after an edit, but then goes on the say that's it's interpreting it. You don't interpret compiled code do you?)

deleted 1 character in body; added 1 character in body
Source Link
Carcigenicate
  • 2.7k
  • 3
  • 26
  • 40

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 identicala built in function will run more then three times faster then an identical, user defined onesone. 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 itsit's still significantly slower.)

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 identical built in function run more then three times faster then identical, user defined ones. 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 its still significantly slower.)

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.)

small numbers get spelled out. Remove thanks (see http://meta.stackexchange.com/questions/2950/ ), the url parser is fairly smart (no extra whitespace needed).
Source Link
user40980
user40980
Loading
Source Link
Carcigenicate
  • 2.7k
  • 3
  • 26
  • 40
Loading