Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exponent operator performance

Tags:

haskell

Okay maybe a silly question here, but I am currently learning haskell by completing problems on projecteuler.net

I ran into an interesting observation and was hoping someone could shed some light why things are the way they are.

For reference, I was implementing Problem #29 Here's what I came up

nub $ [ a^^b | a <- [2..100], b <- [2..100] ]

I observed that using the ^^ operator is faster than ** which is faster than ^ for the input listed above.

My question is simply, why? Each of these operators applies to different type classes. My guess is there is some type conversions taking place, but I would expect ^ to be the faster of the operations, when it seems it's actually the oposite.

Thanks!

like image 775
Jake Avatar asked Jan 18 '23 10:01

Jake


1 Answers

All the time is spent on nub. With ^^ and ** you're doing nub on [Double]. With ^ it is nub on [Integer], and comparing big integers is slower than comparing doubles.

like image 198
Sjoerd Visscher Avatar answered Jan 24 '23 20:01

Sjoerd Visscher