node/benchmark/function_call/bench.js

44 lines
766 B
JavaScript
Raw Normal View History

2010-09-24 10:55:26 +08:00
var binding = require('./build/default/binding');
2010-09-24 17:21:17 +08:00
c = 0
2010-09-24 10:55:26 +08:00
function js() {
2010-09-24 17:21:17 +08:00
return c++; //(new Date()).getTime();
2010-09-24 10:55:26 +08:00
}
var cxx = binding.hello;
2010-09-24 17:21:17 +08:00
var i, N = 100000000;
2010-09-24 10:55:26 +08:00
console.log(js());
console.log(cxx());
var start = new Date();
for (i = 0; i < N; i++) {
js();
}
var jsDiff = new Date() - start;
console.log(N +" JS function calls: " + jsDiff);
var start = new Date();
for (i = 0; i < N; i++) {
cxx();
}
var cxxDiff = new Date() - start;
console.log(N +" C++ function calls: " + cxxDiff);
2010-09-28 16:14:38 +08:00
function toMicro (diff) {
return (diff / N) * 1000000;
}
console.log("\nJS function call speed: %d microseconds", toMicro(jsDiff));
console.log("C++ function call speed: %d microseconds", toMicro(cxxDiff));
2010-09-24 10:55:26 +08:00
console.log("\nJS speedup " + (cxxDiff / jsDiff));