Javascript 比较nodejs c++;插件';与js等效的速度是多少?

Javascript 比较nodejs c++;插件';与js等效的速度是多少?,javascript,c++,performance,node.js,v8,Javascript,C++,Performance,Node.js,V8,我有一个nodejs程序,在其中我做了很多计算。我正在考虑加快速度,所以我决定尝试把一些代码移到C++。但首先我进行了一个快速测试,看看性能提升是否显著 我知道在V8中调用C++函数是很昂贵的,所以在我的测试中只有一个调用!p> binding.gyp文件: { "targets": [ { "target_name": "test", "sources": [ "test.cc" ] } ] } #include <node.h>

我有一个nodejs程序,在其中我做了很多计算。我正在考虑加快速度,所以我决定尝试把一些代码移到C++。但首先我进行了一个快速测试,看看性能提升是否显著

<>我知道在V8中调用C++函数是很昂贵的,所以在我的测试中只有一个调用!p> binding.gyp文件:

{
  "targets": [
    {
      "target_name": "test",
      "sources": [ "test.cc" ]
    }
  ]
}
#include <node.h>
#include <v8.h>
#include <cstdlib>
#include <time.h>

using namespace v8;

Handle<Value> Func(const Arguments& args) {
  HandleScope scope;
  double sum = 0;
  double x = rand() / RAND_MAX;
  double y = rand() / RAND_MAX;

  for (int i = 0; i < 1e9; i += 1) {
    x = x / y;
    y = x * rand() / RAND_MAX;
    sum += x + y;
  }

  return scope.Close(Number::New(sum));
}

void init(Handle<Object> exports) {
  srand(time(NULL));
  exports->Set(String::NewSymbol("func"),
      FunctionTemplate::New(Func)->GetFunction());
}

NODE_MODULE(test, init)
'use strict';

var cpp = require("./build/Release/test").func;

function js() {
  var sum = 0;
  var x = Math.random();
  var y = Math.random();

  for (var i = 0; i < 1e9; i += 1) {
    x = x / y;
    y = x * Math.random();
    sum += x + y;
  }

  return sum;
}

function log(msg, hrtime) {
  console.log(msg, (hrtime[0] * 1e9 + hrtime[1]) * 1e-9);
}

var t = process.hrtime();
js();
log('JS', process.hrtime(t));

t = process.hrtime();
cpp();
log('CPP', process.hrtime(t));
JS 8.060747399 
CPP 15.041201326000001
test.cc文件:

{
  "targets": [
    {
      "target_name": "test",
      "sources": [ "test.cc" ]
    }
  ]
}
#include <node.h>
#include <v8.h>
#include <cstdlib>
#include <time.h>

using namespace v8;

Handle<Value> Func(const Arguments& args) {
  HandleScope scope;
  double sum = 0;
  double x = rand() / RAND_MAX;
  double y = rand() / RAND_MAX;

  for (int i = 0; i < 1e9; i += 1) {
    x = x / y;
    y = x * rand() / RAND_MAX;
    sum += x + y;
  }

  return scope.Close(Number::New(sum));
}

void init(Handle<Object> exports) {
  srand(time(NULL));
  exports->Set(String::NewSymbol("func"),
      FunctionTemplate::New(Func)->GetFunction());
}

NODE_MODULE(test, init)
'use strict';

var cpp = require("./build/Release/test").func;

function js() {
  var sum = 0;
  var x = Math.random();
  var y = Math.random();

  for (var i = 0; i < 1e9; i += 1) {
    x = x / y;
    y = x * Math.random();
    sum += x + y;
  }

  return sum;
}

function log(msg, hrtime) {
  console.log(msg, (hrtime[0] * 1e9 + hrtime[1]) * 1e-9);
}

var t = process.hrtime();
js();
log('JS', process.hrtime(t));

t = process.hrtime();
cpp();
log('CPP', process.hrtime(t));
JS 8.060747399 
CPP 15.041201326000001
为什么C++插件这么慢?
我使用的是node v0.10.21

div操作是最昂贵的,与js代码相比,您使用它的次数多了3次。另外,您不知道random()是如何实现的。来自C++的代码< >随机()/<代码>代码可能与JS中的<代码>随机()/代码>代码非常不同,所以不要做出假设。

< P>“VC++中编写Prof敏感部件”的思想存在缺陷。这在python/php中是有意义的,在python/php中,规范解释器的速度比V8编译的javascript慢10000x以上

如果您在编写JS时考虑到性能,您将很容易达到足够快的水平。虽然编写这样的JS并不容易,因为几乎每个直观的成语都对性能不好。

证明C++ +()函数比Mault.Read()JS函数慢得多。请看这个问题。