From 8444612af15f939b804d6051d08cfa1419fa29bd Mon Sep 17 00:00:00 2001 From: Robert Kaiser Date: Tue, 19 Aug 2008 03:47:41 +0200 Subject: [PATCH] use vastly more efficiant but less beautiful code for the real calc loop --- xulapp/chrome/mandelbrot/content/mandelbrot.js | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/xulapp/chrome/mandelbrot/content/mandelbrot.js b/xulapp/chrome/mandelbrot/content/mandelbrot.js index 4dd2a4b..4409113 100644 --- a/xulapp/chrome/mandelbrot/content/mandelbrot.js +++ b/xulapp/chrome/mandelbrot/content/mandelbrot.js @@ -48,11 +48,25 @@ function complex(aReal, aImag) { } function mandelbrotValue (aC, aIterMax) { - var Z = new complex(0.0, 0.0); + /* XXX: this would be nice code but it looks like JS objects are too heavy for this. + let Z = new complex(0.0, 0.0); for (var iter = 0; iter < aIterMax; iter++) { Z = Z.square().add(aC); if (Z.r * Z.r + Z.i * Z.i > 256) { break; } } + */ + + // highly optimized code for fast calculation + let Cr = aC.r, Ci = aC.i; + let Zr = 0.0, Zi = 0.0; + let Zr2 = Zr * Zr, Zi2 = Zi * Zi; + for (var iter = 0; iter < aIterMax; iter++) { + Zi = 2 * Zr * Zi + Ci; + Zr = Zr2 - Zi2 + Cr; + + Zr2 = Zr * Zr; Zi2 = Zi * Zi; + if (Zr2 + Zi2 > 256) { break; } + } return iter; } -- 2.43.0