Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
var $count = [320, 320 * 2, 320 * 4 ];
var $i = 0;
while (true) {
if ($i > 2) {
break;
}
var $size = $count[$i];
$i += 1;
console.log($i, $size);
var $time = Date.now();
var $byte_acc = 0;
var $bit_num = 0;
var $sum = 0;
var $y = 0;
while ($y < $size) {
var $ci = (2.0 * $y / $size) - 1.0;
var $x = 0;
while ($x < $size) {
var $zr, $zi;
var $zrzr = $zr = 0.0;
var $zizi = $zi = 0.0;
var $cr = (2.0 * $x / $size) - 1.5;
var $escape = 1;
var $z = 0;
while ($z < 50) {
var $tr = $zrzr - $zizi + $cr;
var $ti = 2.0 * $zr * $zi + $ci;
$zr = $tr;
$zi = $ti;
// preserve recalculation
$zrzr = $zr * $zr;
$zizi = $zi * $zi;
if ($zrzr + $zizi > 4.0) {
$escape = 0;
break;
}
$z += 1;
}
$byte_acc = ($byte_acc << 1) | $escape;
$bit_num += 1;
// Code is very similar for these cases,
// but using separate blocks
// ensures we skip the shifting when it's unnecessary,
// which is most cases.
if ($bit_num == 8) {
$sum += $bit_num + $byte_acc;
$byte_acc = 0;
$bit_num = 0;
} else if ($x == $size - 1) {
$byte_acc <<= (8 - $bit_num);
$sum += $bit_num + $byte_acc;
$byte_acc = 0;
$bit_num = 0;
}
$x += 1;
}
$y += 1;
}
console.log($sum);
console.log((Date.now() - $time) / 1000);
}
wicked@wnote:~/mandelbrot$ node 1.js
1 320
1394601
0.217
2 640
5586324
0.108
3 1280
22350292
0.43
wicked@wnote:~/mandelbrot$ php 1.php
1 : 320
1394601
1.0391900539398
2 : 640
5586324
4.1505010128021
3 : 1280
22350292
16.621148109436
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.