Add function_call benchmark
authorRyan Dahl <ry@tinyclouds.org>
Fri, 24 Sep 2010 02:55:26 +0000 (19:55 -0700)
committerRyan Dahl <ry@tinyclouds.org>
Fri, 24 Sep 2010 05:27:44 +0000 (22:27 -0700)
benchmark/function_call/bench.js [new file with mode: 0644]
benchmark/function_call/binding.cc [new file with mode: 0644]
benchmark/function_call/wscript [new file with mode: 0644]

diff --git a/benchmark/function_call/bench.js b/benchmark/function_call/bench.js
new file mode 100644 (file)
index 0000000..39f76ca
--- /dev/null
@@ -0,0 +1,33 @@
+var binding = require('./build/default/binding');
+
+function js() {
+  return (new Date()).getTime();
+}
+
+var cxx = binding.hello;
+
+var i, N = 10000000;
+
+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);
+
+console.log("\nJS speedup " + (cxxDiff / jsDiff)); 
+
+
diff --git a/benchmark/function_call/binding.cc b/benchmark/function_call/binding.cc
new file mode 100644 (file)
index 0000000..e33d590
--- /dev/null
@@ -0,0 +1,17 @@
+#include <v8.h>
+#include <node.h>
+#include <time.h>
+
+using namespace v8;
+
+static Handle<Value> Hello(const Arguments& args) {
+  HandleScope scope;
+  time_t tv = time(NULL);
+  return scope.Close(Integer::New(tv));
+}
+
+extern "C" void init (Handle<Object> target) {
+  HandleScope scope;
+  //target->Set(String::New("hello"), String::New("World"));
+  NODE_SET_METHOD(target, "hello", Hello);
+}
diff --git a/benchmark/function_call/wscript b/benchmark/function_call/wscript
new file mode 100644 (file)
index 0000000..3db367f
--- /dev/null
@@ -0,0 +1,15 @@
+srcdir = '.'
+blddir = 'build'
+VERSION = '0.0.1'
+
+def set_options(opt):
+  opt.tool_options('compiler_cxx')
+
+def configure(conf):
+  conf.check_tool('compiler_cxx')
+  conf.check_tool('node_addon')
+
+def build(bld):
+  obj = bld.new_task_gen('cxx', 'shlib', 'node_addon')
+  obj.target = 'binding'
+  obj.source = 'binding.cc'