Skip to content

Commit eb4a95b

Browse files
committed
refactor for node-gyp
1 parent 414893c commit eb4a95b

File tree

8 files changed

+87
-85
lines changed

8 files changed

+87
-85
lines changed

.travis.yml

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
language: node_js
2+
node_js:
3+
- 0.10
4+
before_install:
5+
- npm install -g node-gyp
6+
before_script:
7+
- node-gyp configure build
8+

README.md

+22-14
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,31 @@
11
# Node.js modules speed test: C++ vs JavaScript
22

3-
Download, build and test instructions:
3+
[![Build Status](https://travis-ci.org/lupomontero/node-cppspeed.svg?branch=master)](https://travis-ci.org/lupomontero/node-cppspeed)
44

5-
$ git clone git://github.com/lupomontero/node-cppspeed.git
6-
...
5+
## Download and build
76

8-
$ cd node-cppspeed
9-
$ node-waf configure build
10-
...
7+
```sh
8+
# Install node-gyp globally if not installed yet
9+
npm install -g node-gyp
1110

12-
$ node test/cppspeed.js
11+
# Clone, build and test
12+
git clone git://github.com/lupomontero/node-cppspeed.git
13+
cd node-cppspeed
14+
node-gyp configure build
15+
```
1316

14-
foo 1 run in 171ms
15-
foo 2 run in 109ms
16-
cppfoo run in 19ms
17-
c++ was 9.0 times faster (fooTime1)
18-
c++ was 5.7 times faster (fooTime2)
17+
## Run
1918

19+
```sh
20+
npm test
21+
```
2022

23+
You should see output like below:
2124

22-
[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/lupomontero/node-cppspeed/trend.png)](https://bitdeli.com/free "Bitdeli Badge")
23-
25+
```
26+
foo 1 run in 100ms
27+
foo 2 run in 59ms
28+
cppfoo run in 5ms
29+
c++ was 20.0 times faster (fooTime1)
30+
c++ was 11.8 times faster (fooTime2)
31+
```

binding.gyp

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"targets": [
3+
{
4+
"target_name": "cppspeed",
5+
"sources": [ "cppspeed.cpp" ]
6+
}
7+
]
8+
}

cppspeed.cpp

+5-10
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,8 @@
44

55
using namespace std;
66
using namespace v8;
7-
using namespace node;
87

9-
static Handle<Value> foo(const Arguments& args)
10-
{
8+
static Handle<Value> foo(const Arguments& args) {
119
int i, j;
1210
string str;
1311

@@ -19,11 +17,8 @@ static Handle<Value> foo(const Arguments& args)
1917
return String::New("something");
2018
}
2119

22-
extern "C" {
23-
static void init(Handle<Object> target)
24-
{
25-
NODE_SET_METHOD(target, "foo", foo);
26-
}
27-
28-
NODE_MODULE(cppspeed, init);
20+
void init(Handle<Object> exports) {
21+
NODE_SET_METHOD(exports, "foo", foo);
2922
}
23+
24+
NODE_MODULE(cppspeed, init)

package.json

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"name": "cppspeed",
3+
"version": "1.0.0",
4+
"description": "Node.js modules speed test: C++ vs JavaScript",
5+
"main": "./build/Release/cppspeed.node",
6+
"scripts": {
7+
"test": "node test.js"
8+
},
9+
"gypfile": true
10+
}

test.js

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
var cppfoo = require('./').foo;
2+
3+
4+
function foo() {
5+
var i, j = 0, str = '';
6+
for (i = 0; i < 1000000; i++) {
7+
j = i + 1;
8+
str += 'a';
9+
}
10+
return "something";
11+
}
12+
13+
14+
var start, fooTime1, fooTime2, cppfooTime;
15+
16+
start = +new Date();
17+
foo();
18+
fooTime1 = +new Date() - start;
19+
20+
start = +new Date();
21+
foo();
22+
fooTime2 = +new Date() - start;
23+
24+
start = +new Date();
25+
cppfoo();
26+
cppfooTime = +new Date() - start;
27+
28+
29+
console.log('foo 1 run in ' + fooTime1 + 'ms');
30+
console.log('foo 2 run in ' + fooTime2 + 'ms');
31+
console.log('cppfoo run in ' + cppfooTime + 'ms');
32+
console.log('c++ was ' + (fooTime1/cppfooTime).toFixed(1) + ' times faster (fooTime1)');
33+
console.log('c++ was ' + (fooTime2/cppfooTime).toFixed(1) + ' times faster (fooTime2)');
34+

test/cppspeed.js

-33
This file was deleted.

wscript

-28
This file was deleted.

0 commit comments

Comments
 (0)