[Haskell-cafe] Performance Issue

Daniel Fischer daniel.is.fischer at web.de
Sat May 22 11:06:32 EDT 2010


On Saturday 22 May 2010 16:48:27, Daniel Fischer wrote:
> The boxing is due to the use of (^).
> If you write x*x instead of x^2, it can use the primop *## and needn't
> box it.
> As a side effect, the original time leak probably wouldn't have occured
> with x*x instead of x^2 because one would've made it
>    let x = newton a (n-1) in (x*x +a) / (2*x)
> instead of writing out newton a (n-1) thrice anyway, wouldn't one?
>

Even if. With

newton :: Double -> Int -> Double
newton a 0 = a
newton a n =
    (((newton a (n-1)) * (newton a (n-1)) ) + a)/(2*(newton a (n-1)))

(and optimisations of course), GHC does share newton a (n-1).

Lesson: Writing x^2 is a baad thing.


More information about the Haskell-Cafe mailing list