From: Robert Kaiser Date: Tue, 19 Aug 2008 01:47:41 +0000 (+0200) Subject: use vastly more efficiant but less beautiful code for the real calc loop X-Git-Url: https://git-public.kairo.at/?p=mandelbrot.git;a=commitdiff_plain;h=8444612af15f939b804d6051d08cfa1419fa29bd use vastly more efficiant but less beautiful code for the real calc loop --- 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; }