Last active
December 13, 2015 18:58
-
-
Save you21979/4959183 to your computer and use it in GitHub Desktop.
指定した時間、処理を繰り返し実行し、実行できた回数を表示するベンチマーク関数
nodejs専用
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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);});
一回しか実行できなかったので↑みたいに実行できるように修正した