Created
August 20, 2012 09:33
-
-
Save dead-horse/3402655 to your computer and use it in GitHub Desktop.
两种传递buffer的方法
This file contains 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
#include <node.h> | |
#include <v8.h> | |
#include <node_buffer.h> | |
#include <iostream> | |
using namespace std; | |
using namespace v8; | |
using namespace node; | |
Handle<Value> slowBuffer(const Arguments& args) { | |
HandleScope scope; | |
char a[100] = {-2, 0, 1, 2, 0, 0, 0, 0, -127, 32, 0, 0, 0, -52, 77, 116, 0, 42, 99, 111, | |
-2, 0, 1, 2, 0, 0, 0, 0, -127, 32, 0, 0, 0, -52, 77, 116, 0, 42, 99, 111, | |
-2, 0, 1, 2, 0, 0, 0, 0, -127, 32, 0, 0, 0, -52, 77, 116, 0, 42, 99, 111, | |
-2, 0, 1, 2, 0, 0, 0, 0, -127, 32, 0, 0, 0, -52, 77, 116, 0, 42, 99, 111, | |
-2, 0, 1, 2, 0, 0, 0, 0, -127, 32, 0, 0, 0, -52, 77, 116, 0, 42, 99, 111 | |
}; | |
Buffer *buf = Buffer::New(a, 100); | |
return scope.Close(buf->handle_); | |
} | |
Handle<Value> array(const Arguments& args) { | |
HandleScope scope; | |
char a[100] = {-2, 0, 1, 2, 0, 0, 0, 0, -127, 32, 0, 0, 0, -52, 77, 116, 0, 42, 99, 111, | |
-2, 0, 1, 2, 0, 0, 0, 0, -127, 32, 0, 0, 0, -52, 77, 116, 0, 42, 99, 111, | |
-2, 0, 1, 2, 0, 0, 0, 0, -127, 32, 0, 0, 0, -52, 77, 116, 0, 42, 99, 111, | |
-2, 0, 1, 2, 0, 0, 0, 0, -127, 32, 0, 0, 0, -52, 77, 116, 0, 42, 99, 111, | |
-2, 0, 1, 2, 0, 0, 0, 0, -127, 32, 0, 0, 0, -52, 77, 116, 0, 42, 99, 111 | |
}; | |
Handle<Array> bufArray = v8::Array::New(100); | |
for(unsigned i=0; i!=100; ++i) { | |
bufArray->Set(i, v8::Integer::New((short)a[i])); | |
} | |
return scope.Close(bufArray); | |
} | |
void init(Handle<Object> target) { | |
target->Set(String::NewSymbol("slowBuffer"), | |
FunctionTemplate::New(slowBuffer)->GetFunction()); | |
target->Set(String::NewSymbol("array"), | |
FunctionTemplate::New(array)->GetFunction()); | |
} | |
NODE_MODULE(buf, init) | |
This file contains 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 bufTest = require('./build/Release/buf'); | |
console.time('slowBuffer'); | |
for (var i = 0 ; i < 100000; i++) { | |
bufTest.slowBuffer(); | |
} | |
console.timeEnd('slowBuffer'); //200+ms | |
console.time('array'); | |
for (var i = 0 ; i < 100000; i++) { | |
new Buffer(bufTest.array()); | |
} | |
console.timeEnd('array'); //1000+ms |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment