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:
Anyone that have experience in these type of computation on both platform?
C++ uses the CPU and performs up to 10X faster than Node.
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.
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.
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.
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.
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.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With