request for critique
Richard A. O'Keefe
ok at cs.otago.ac.nz
Tue Jun 3 05:10:38 UTC 2003
Concerning efficiency, let's look at one of the methods:
iterationsSurvivedBy: aPoint
"Answer how many iterations of
z(0) = 0
z(i+1) = z(i) squared + aPoint
it took to discover that aPoint is not in the Mandelbrot set.
Answer -1 if aPoint was not ruled out before the maximum
number of iterations was executed."
| a b |
a := aPoint x.
b := aPoint y.
1 to: maxNumberOfIterations do: [:iteration| |aSquared bSquared oldA|
aSquared := a squared.
bSquared := b squared.
aSquared + bSquared > 4 ifTrue: [^iteration].
oldA := a.
a := aSquared - bSquared + aPoint x.
b := 2 * oldA * b + aPoint y].
^-1.
! !
I am a little puzzled that the comment says that iterations start
with (complex) 0 but the code starts with (aPoint x, aPoint y).
I note that having extracted aPoint x and aPoint y,
they are extracted again and again and again.
This would be more efficient as
iterationsSurvivedBy: aPoint
|a b x y|
a := x := aPoint x.
b := y := aPoint y.
1 to: maxNumberOfIteractions do: [:iteration| |a2 b2|
a2 := a * a.
b2 := b * b.
a2 + b2 > 4 ifTrue: [^iteration].
b := 2 * a * b + y.
a := a2 - b2 + x].
^-1
Yes, x*x is marginally faster than x squared (saves a message send).
For a calculation like this, eliminating the repeated calls to
Point {x,y} and #squared may be worth it, generally one wouldn't bother.
More information about the Squeak-dev
mailing list
|