Skip to content

Commit 5c0cd1e

Browse files
committed
perf: do not calculate square root when calculate modulus
1 parent 4d431fe commit 5c0cd1e

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

wasm/src/lib.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ pub struct Mandelbrot {
77
pixels_count: usize,
88
}
99

10-
const ESCAPE_MODULUS: f64 = 2.0;
10+
/// 2^2
11+
const ESCAPE_MODULUS: f64 = 4.0;
1112
/// complex zero
1213
const Z0: Complex<f64> = Complex::new(0.0, 0.0);
1314

@@ -82,7 +83,8 @@ fn check_series(x: f64, i: f64, depth: u32) -> u32 {
8283
let mut num = Z0 + point;
8384

8485
for step in 0..depth {
85-
if num.norm() >= ESCAPE_MODULUS {
86+
// do not calculate square root
87+
if num.norm_sqr() >= ESCAPE_MODULUS {
8688
return step;
8789
}
8890

0 commit comments

Comments
 (0)