Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js vs C++ for mathematic

Tags:

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?

like image 349
Pablosproject Avatar asked Aug 30 '12 11:08

Pablosproject


People also ask

Is NodeJS faster than C++?

C++ uses the CPU and performs up to 10X faster than Node.

Is NodeJS better than C#?

Language: Node. js is based on JavaScript. There's no denying that by offering a strict type system and compile-time error checks, C# is more potent than JavaScript, which relies on Facebook's Flow static type checker or Microsoft TypeScript. Moreover, C# grows more efficient and expressive with every generation.

Is NodeJS good for computation?

CPU-heavy server-side computation Although Node. js is versatile and powerful, heavy computations are not its strong point. It plunges the CPU into an intensive bout of number crunching, which blocks incoming requests from being processed.

How much faster is C than JavaScript?

That means, if you're on such a system, you're talking in microseconds for C and milliseconds for JavaScript (as per the JS online docs). So, rather than JS being four times faster, C++ is actually 250 times faster.


2 Answers

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.

like image 171
Tobias Langner Avatar answered Oct 17 '22 21:10

Tobias Langner


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

  • primes.c
  • primes.js

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

like image 31
Tom Palmer Avatar answered Oct 17 '22 22:10

Tom Palmer