Odd Performance
Tom Pledger
Tom.Pledger@peace.com
Wed, 23 Oct 2002 10:22:49 +1300
Tim Otten writes:
:
| Can anyone suggest why the tighter algorithm exhibits significantly
| worse performance? Is takeWhile significicantly more expensive than
| take?
No.
| Is the \z lambda expression expensive?
No.
| The intsqrt isn't recalculated each time takeWhile evalutes a
| prime, is it?
Probably. Try replacing this
(\z -> z <= (intsqrt x))
with this
(\z -> z^2 <= x)
or moving the O(sqrt(n)) step[1] into a let expression
let zmax = intsqrt x in ... (\z -> z <= zmax) ...
| Slightly confused,
| Tim Otten
|
| [1] Note that intsqrt calculates the floor of the square root of x.
| intsqrt x = intsqrt' 1 x
| intsqrt' n x
| | (n*n > x) = n-1
| | otherwise = intsqrt' (n+1) x