Skip to content

Instantly share code, notes, and snippets.

@you21979
Last active December 13, 2015 18:58
Show Gist options
  • Save you21979/4959183 to your computer and use it in GitHub Desktop.
Save you21979/4959183 to your computer and use it in GitHub Desktop.
指定した時間、処理を繰り返し実行し、実行できた回数を表示するベンチマーク関数 nodejs専用
var benchmark = module.exports = function(f, timeout){
var i = 0;
var deadline = process.uptime() + timeout;
while(1){
if(process.uptime() >= deadline){
break;
}
f();
++i;
}
return i;
};
if(!module.parent){
var ret = benchmark(function(){
var arraytest = new Array(100);
}, 5.0);
console.log("done [%d]", ret);
}
@you21979
Copy link
Author

var bm = require('./benchmark');
[
function(timeout){
console.log("go1");
var r = bm(function(){
new Array(["aaaaaaaa"]);
}, timeout);
console.log("go1 [%d]",r);
},
function(timeout){
console.log("go2");
var r = bm(function(){
new Buffer("aaaaaaaa");
}, timeout);
console.log("go2 [%d]",r);
}
].forEach(function(f){f(5.0);});

一回しか実行できなかったので↑みたいに実行できるように修正した

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment