Skip to content

Commit 6ec22da

Browse files
authored
Improve performance of Mandelbrot iteration (#14)
* Skip obsolete escape check for start value z0 * Drop complex number primitive to avoid expensive logarithm operation
1 parent 8ddb4a8 commit 6ec22da

File tree

1 file changed

+5
-6
lines changed

1 file changed

+5
-6
lines changed

mandelbrot.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package main
33
import (
44
"image/color"
55
"math"
6-
"math/cmplx"
76
)
87

98
func getColorForComplexNr(c complex128) color.RGBA {
@@ -30,12 +29,12 @@ func getColorFromMandelbrot(isUnlimited bool, magnitude float64, iterations int)
3029
func runMandelbrot(c complex128) (bool, float64, int) {
3130
var z complex128
3231

33-
for i := 0; i < imgConf.MaxIter; i++ {
34-
magnitude := cmplx.Abs(z)
35-
if magnitude > 2 {
36-
return true, magnitude, i
37-
}
32+
for i := 1; i < imgConf.MaxIter; i++ {
3833
z = z*z + c
34+
magnitudeSquared := real(z)*real(z) + imag(z)*imag(z)
35+
if magnitudeSquared > 4 {
36+
return true, math.Sqrt(magnitudeSquared), i
37+
}
3938
}
4039
return false, 0, 0
4140
}

0 commit comments

Comments
 (0)