Ulam spiral generators

JavaScript performance comparison

Test case created by Joseph Adams


Both me and my dad wrote a ulam matrix generator, and mine is bigger when compiled with closure. So I now want to prove that mine is faster.

Preparation code

function ulam_qmacro(b,a,e,f){if(b==a||b==a+1)if(0<e&&e<=b&&0<f&&f<=a){a=[Math.floor(b/2)+b%2,Math.floor(a/2)+1];b=a[0];a=a[1];var g=1,h=1,c=1,d=0,j=1,k=1;do{for(var l=0;2>l;l++){for(var m=0;m<h;m++){if(b==e&&a==f)return g;g++;b+=c;a+=d}0==c?(c=-1*j,k=d,d=0):(d=-1*k,j=c,c=0)}h++}while(9999999>g)}else console.log("Invalid PX and PY combination");else console.log("Invalid W and H combination")};

function f(b,a){if(0===a%2)return b[0]!=a&&1!=b[1]?!0:!1;if(1===a%2)return 1!=b[0]&&b[1]!=a?!0:!1}
function ulam_jcla1(b,a){var c;1>b[0]||1>b[1]||b[0]>a||b[1]>a||1>a?(console.log("Invalid values!"),c=!1):c=!0;if(!c)return-1;for(;f(b,a);){c=b;var d=a;0===d%2?c[1]-=1:1===d%2&&(c[0]-=1);b=c;a-=1}c=-1;var d=b[0],e=b[1];0===a%2?1===b[1]?c=Math.pow(a,2)-(b[0]-1):b[1]===a?c=Math.pow(a,2)-2*(a-1)-(a-2)+(d-1)-1:1===b[0]?c=Math.pow(a,2)-3*(a-1)-(a-2)+(e-2):b[0]===a&&(c=Math.pow(a,2)-a-(e-2)):1===a%2&&(1===b[1]?c=Math.pow(a,2)-2*(a-1)-(d-1):b[1]===a?c=Math.pow(a,2)-(a-1)+(d-1):1===b[0]?c=Math.pow(a,2)-2*
(a-1)+(e-1):b[0]===a&&(c=Math.pow(a,2)-3*(a-1)-(e-1)));return c};

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
jcla1's code small numbers
for(var i = 0; i < 10000; i++){ulam_jcla1([3, 5], 6);}
qmacro's code small numbers
for(var i = 0; i < 10000; i++){ulam_qmacro(6,6,3,5);}
jcla1's code big numbers
ulam_jcla1([154823, 157623], 1548230);
qmacro's code big numbers
ulam_qmacro(1548230, 1548230, 154823, 157623 );
jcla1's code big numbers center
for(var i = 0; i < 100; i++){ulam_jcla1([50, 50], 1548230);}
qmacro's code big numbers center
for(var i = 0; i < 100; i++){ulam_qmacro(1548230, 1548230, 50, 50 );}

Compare results of other browsers


You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:


Add a comment