Node.js 与 C++ 的数学

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12195841/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 15:59:59  来源:igfitidea点击:

Node.js vs C++ for mathematic

c++performancenode.jsmathcomputation

提问by Pablosproject

I have to write a server program that implements some fuzzy logic and I choose to write it in Node.js to take advantage of its event orientation. I have to work with difficult mathematic computational problem, and I don't know what's the best way to obtain performance:

我必须编写一个实现一些模糊逻辑的服务器程序,我选择在 Node.js 中编写它以利用其面向事件的优势。我必须处理困难的数学计算问题,我不知道获得性能的最佳方法是什么:

  1. Write all in Node.jsand use the power of the V8 engine for the mathematical task.
  2. Write a module in C++that implements all the mathematical function and call it from Node.
  1. 全部用 Node.js 编写,并使用 V8 引擎的强大功能来完成数学任务。
  2. 用 C++ 编写一个实现所有数学函数的模块,并从 Node.js 调用它。

Anyone that have experience in these type of computation on both platform?

任何在这两个平台上都具有此类计算经验的人?

回答by Tobias Langner

Since you need the Node.js part anyway, go ahead, implement everything in Node.js. If it is fast enough, this is easy enough to maintain. It's very hard to predict the power of a virtual machine / JIT compiler.

由于无论如何您都需要 Node.js 部分,请继续,在 Node.js 中实现所有内容。如果它足够快,这很容易维护。很难预测虚拟机/JIT 编译器的能力。

If it is not fast enough, first think about algorithmic improvements. If this doesn't help and if profiling shows that the computation is the problem, go ahead, re-implement it in C++. But be aware that writing performant C++ code is not trivial. Make sure that you have a good profiler at hand and measure often.

如果还不够快,首先考虑算法改进。如果这没有帮助,并且如果分析表明计算是问题,请继续,用 C++ 重新实现它。但请注意,编写高性能 C++ 代码并非易事。确保您手头有一个好的分析器并经常测量。

In general I'd say C++ code is faster if written correctly. The tricky part is to write it correctly. Please check this article Google Paper on C++, Java, Scala, Gofor more information. The gist is - managed languages make it a lot easier to write & maintain the code but if you need raw performance, C++ is the best. But it comes at the price of needing a lot of expertise and that the code is harder to maintain.

一般来说,如果编写正确,我会说 C++ 代码会更快。棘手的部分是正确编写它。请查看这篇关于 C++、Java、Scala、Go 的 Google 论文以获取更多信息。要点是 - 托管语言使编写和维护代码变得更加容易,但是如果您需要原始性能,C++ 是最好的。但它的代价是需要大量的专业知识,而且代码更难维护。

回答by Tom Palmer

denshade, your C implementation goes only to 2e5 not 2e6, like you've done for js (linking to today's revs on Github):

denshade,您的 C 实现仅适用于 2e5 而不是 2e6,就像您为 js 所做的那样(链接到今天在 Github 上的 revs):

Piping to /dev/null, and changing js also to 2e5, I get about 6.5 seconds for C and about 8.5 seconds for js (using some version of node) on my current computer.

管道到 /dev/null,并将 js 也更改为 2e5,我在我当前的计算机上得到大约 6.5 秒的 C 和大约 8.5 秒的 js(使用某些版本的节点)。

Since your algorithm is O(n^2), I would expect 2e6 to take some 15 minutes, not 15 hours, but I haven't tried it. Maybe it does fall apart that bad for some reason.

由于您的算法是 O(n^2),我预计 2e6 需要大约 15 分钟,而不是 15 小时,但我还没有尝试过。也许由于某种原因它确实崩溃了。

(Note that I couldn't comment directly, since I'm brand new on SO and have no rep.)

(请注意,我无法直接发表评论,因为我是全新的 SO 并且没有代表。)

回答by jcoder

it's pretty much impossible to answer this kind of question. The answer as always for these things is it depends on your skills and how much time and effort you are willing to put into it.

回答这种问题几乎是不可能的。这些事情的答案一如既往地取决于您的技能以及您愿意投入多少时间和精力。

C++ always has the potential to be faster and more efficient as you have much closer control over all the things that matter. The downside it that you haveto do all the things that matter and the generic implementations in the other language are probably done by someone who knows what they are doing and could well be better than a naive or quick implementation in c++

C++ 总是具有更快、更高效的潜力,因为您可以更密切地控制所有重要的事情。不利的一面是,您必须做所有重要的事情,而其他语言中的通用实现可能是由知道自己在做什么的人完成的,并且可能比 C++ 中的幼稚或快速实现要好

Plus often you'll find that the bottleneck isn't what you think it will be anyway, for example if reading in your data turns out to take 20 times as long as the calculations which isn't impossible then it hardly matters how fast the calculations are. And intuition about where the bottlenecks lie is often badly wrong even for experienced developers.

此外,您经常会发现瓶颈并不是您认为的那样,例如,如果读取数据的时间是计算的 20 倍,这并非不可能,那么计算速度有多快几乎无关紧要。计算是。即使对于有经验的开发人员来说,关于瓶颈所在的直觉也常常是错误的。

回答by Head Wizard Locke

http://benchmarksgame.alioth.debian.org/u64q/compare.php?lang=node&lang2=gpp

The above link is dead and now in the wayback--

上面的链接已经死了,现在回不去了--

https://web.archive.org/web/20180324192118/http://benchmarksgame.alioth.debian.org/u64q/compare.php?lang=node&lang2=gpp

https://web.archive.org/web/20180324192118/http://benchmarksgame.alioth.debian.org/u64q/compare.php?lang=node&lang2=gpp

C++ uses the CPU and performs up to 10X faster than Node.js doing mathematical operations.

C++ 使用 CPU,执行数学运算的速度比 Node.js 快 10 倍。

That site moved over here https://benchmarksgame-team.pages.debian.net/benchmarksgame/which-programs-are-fastest.html

该网站移至此处 https://benchmarksgame-team.pages.debian.net/benchmarksgame/which-programs-are-fastest.html

回答by SmacL

One thing to consider with going the C++ route for complex mathematical computations is that you might be able to leverage an existing high performance library, such as BLAS, LAPACK, ARMA, etc.. where other developers have already put significant time and effort into providing highly optimized functionality. I doubt you'll find a similar level of high performance library for JavaScript. Certainly if you have an identified bottleneck around matrix calculations or linear algebra, one of these C++ libraries is the way to go.

使用 C++ 进行复杂数学计算时要考虑的一件事是,您可能能够利用现有的高性能库,例如BLAS、LAPACK、ARMA 等。其他开发人员已经投入大量时间和精力来提供高度优化的功能。我怀疑您会为 JavaScript 找到类似级别的高性能库。当然,如果您在矩阵计算或线性代数方面遇到了瓶颈,那么这些 C++ 库之一就是您要走的路。

回答by Yehonal

I've runned @denshade codes removing prints and the timing on 100000 numbers are exceptional:

我已经运行了@denshade 代码来删除打印和 100000 个数字的时间是例外:

  • 3 sec.for nodejs!

  • 6 sec.for gcc/clang compiled c

  • 6 sec. for hhvm ( php )

  • 14 secfor php7 w/ opcache

  • 15 secfor php7 w/o opcache

  • 3 秒。对于 nodejs!

  • 6 秒。对于 gcc/clang 编译的 c

  • 6 秒。对于 hhvm ( php )

  • 带有 opcache 的 php7 需要14 秒

  • 没有opcache 的 php7 需要15 秒

Nodejs is so fast because it's compiled and optimized over-time.

Nodejs 之所以如此之快,是因为它会随着时间的推移进行编译和优化。

so, maybe you just need to test by your self which is the best language that fits your needs in this case.

所以,也许你只需要自己测试一下,在这种情况下,哪种语言最适合你的需求。

回答by denshade

If your calculations aren't trivial I'd like to issue a warning. JavaScript is very bad when you are going for heavy calculations. My story involves a simple prime program which you can find here: https://github.com/denshade/speedFun

如果您的计算不重要,我想发出警告。当您进行大量计算时,JavaScript 非常糟糕。我的故事涉及一个简单的素数程序,你可以在这里找到:https: //github.com/denshade/speedFun

Long story short. I created a simple, be it inefficient prime check function implemented in C & JavaScript. Both are implemented in the same way. The first 2000 000 primes are verified in 5 seconds in C. The same function in javascript lasted for more than 16 hours when run in node.js.

长话短说。我创建了一个简单的,在 C 和 JavaScript 中实现的低效素数检查函数。两者都以相同的方式实现。前 2000 000 个素数在 C 中在 5 秒内被验证。 javascript 中的相同函数在 node.js 中运行时持续了 16 多个小时。

回答by Musa

Following are the areas where Node.js is proving itself as a perfect technology partner.

以下是 Node.js 证明自己是完美技术合作伙伴的领域。

● I/O bound Applications
● Data Streaming Applications
● Data Intensive Real-time Applications (DIRT)
● JSON APIs based Applications
● Single Page Applications

It is not advisable to use Node.js for CPU intensive applications.

不建议将 Node.js 用于 CPU 密集型应用程序。

here is API comparisons : https://www.linkedin.com/pulse/nodejs-vs-java-which-faster-apis-owen-rubel

这是 API 比较:https: //www.linkedin.com/pulse/nodejs-vs-java-which-faster-apis-owen-rubel