Skip to content

Instantly share code, notes, and snippets.

@makotom
Created August 12, 2013 15:17
Show Gist options
  • Save makotom/6211759 to your computer and use it in GitHub Desktop.
Save makotom/6211759 to your computer and use it in GitHub Desktop.
JS Benchmark using Gauss–Legendre
(function(){
"use strict";
var n = 100000,
init = function(){
return {
a : 1,
b : 1 / Math.sqrt(2),
t : 1 / 4,
p : 1,
iter : function(){
var ca = this.a;
this.a = (this.a + this.b) / 2;
this.b = Math.sqrt(ca * this.b);
this.t = this.t - (this.p * Math.pow(ca - this.a, 2));
this.p = 2 * this.p;
return;
},
getPi : function(){
return Math.pow(this.a + this.b, 2) / (4 * this.t);
}
};
},
d = new Date();
(function(){
var i = 0, j = 0, pi = {};
for(i = 0; i < n; i += 1){
pi = init();
for(j = 0; j < 1000; j += 1){
pi.iter();
}
pi.getPi();
}
})();
console.log(new Date().getTime() - d.getTime());
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment