Node.js vs C++ for mathematic
Asked Answered
I

8

28

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:

  1. Write all in Node.js and 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.

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

Infusive answered 30/8, 2012 at 11:27 Comment(6)
Writing it in c++ is clearly going to be faster (if written correctly). It really depends upon what performance level is acceptable. Maybe try writing it in node.js first and then profile it to see if it is a bottleneck.Interlock
"use the power of the V8 engine" made me laugh.Edwinedwina
If you going to do 2+2, trust me node will be at its best. However if you going to do big matrices manipulation, C or C++ is recommended. I'm sure you got my point.Tsaritsyn
Can do both - nodejs.org/api/addons.htmlUnmerciful
2+2 may not mean node is the best option.Knobby
github.com/nodejs/node-addon-apiBangup
K
52

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.

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.

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, Go for 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.

Kirby answered 30/8, 2012 at 11:45 Comment(3)
Agreed 100%. I read an article a week or so ago showing JavaScript dramatically outperforming Java on some numerical code. It's really hard to predict in which cases a JIT compiler or interpreter will perform well. But always start with the easy approach that might work. You can almost certainly get a Javascript implementation up and running much, much faster than a C++ implementation. So start with that, and reimplement it in C++ if you have toDetoxify
@jalf I'd, say, it's not hard to predict jit deoptimizations, but it's hard to debug mistakes. Javascript now has no convenient instruments for such tasks. You can look into debug traces, but it's very boring. If you have very complex library or need 64-bit ints/double floats - it can be more fast to make bindings directly to C/C++. But for simple cases i agree that writing js first is more optimal.Effeminate
You can certainly get a C++ implementation up amd running much, much faster than a Javascript implementation. So start with that and you'll never have to use JS again except for WebAssembly bootstrappingFuturistic
A
25

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

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.

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.

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

Advertence answered 4/9, 2013 at 19:1 Comment(0)
K
9
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

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

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

Knobby answered 1/1, 2017 at 17:32 Comment(0)
H
8

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 have to 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++

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.

Hygro answered 30/8, 2012 at 11:55 Comment(1)
It's not true that in C++ you have to do something that JS already solved it. The standard C++ comes with everything you need to write such program and it's written by very skilled professionals that are hardly matched by a JS programmer.Futuristic
H
4

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.

Hemminger answered 4/3, 2016 at 14:52 Comment(0)
H
3

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

  • 3 sec. for nodejs!

  • 6 sec. for gcc/clang compiled c

  • 6 sec. for hhvm ( php )

  • 14 sec for php7 w/ opcache

  • 15 sec for php7 w/o opcache

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

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

Headstall answered 3/3, 2016 at 16:17 Comment(1)
The problem with NodeJS is that you don't have proper multithread support, but you can always use workers/child processes for that reason. Depending on your needs.Headstall
T
1

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

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.

Tussle answered 25/8, 2013 at 19:31 Comment(2)
I just ran your code verbatim but without the print statements and got 565 seconds... perhaps I/O buffering in printf() is more rigorous than sys.stdout.write? Additionally, if the C code took a long time, perhaps your test mostly tests IO anyway, which is probably not what you want.Slating
Yes, this test is useless if the primes are all output to the console. A true comparison would involve only calculation of n primes. I've performed the same test using cout versus printf(), and printf() performs much better. This test is flawed because its measurements include the performances of JavaScript's process.stdout.write() and C's printf().Jointworm
D
0

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

● 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.

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

Dinka answered 4/3, 2016 at 14:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.