daily pastebin goal
22%
SHARE
TWEET

NodeJS mandelbrot bench

a guest Jun 14th, 2018 64 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Complex {
  2.   constructor(x, y) {
  3.     this.x = x;
  4.     this.y = y;
  5.   }
  6.  
  7.   /** Return the multiplication of this and another complex num */
  8.   mul(other) {
  9.     return new Complex(other.x * this.x - other.y * this.y,
  10.                        other.x * this.y + other.y * this.x);
  11.   }
  12.  
  13.   add(other) {
  14.     return new Complex(other.x + this.x, other.y + this.y);
  15.   }
  16.  
  17.   len_2() {
  18.     return this.x * this.x + this.y * this.y;
  19.   }
  20. }
  21.  
  22. function mandel(c) {
  23.   let iter = function(z) {
  24.     return (z.mul(z)).add(c);
  25.   };
  26.   let z = new Complex(0, 0);
  27.   for (let ii = 0; ii < 100000; ++ii) {
  28.     z = iter(z);
  29.     if (z.len_2() > 4) {
  30.       return ii;
  31.     }
  32.   }
  33.   return -1;
  34. }
  35.  
  36. let start = (new Date()).getTime();
  37. let values = [];
  38. for (let ii = 0; ii < 100; ++ii) {
  39.   values.push([]);
  40.   for (let jj = 0; jj < 100; ++jj) {
  41.     let c = new Complex(ii / 20, jj / 20);
  42.     values[values.length - 1].push(mandel(c));
  43.   }
  44. }
  45. let end = (new Date()).getTime();
  46.  
  47. console.log("Result: " + values);
  48. console.log("Time taken (millis): " + (end - start));
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top