Why is this NodeJS 2x faster than native C?

JoshJ

For the sake of a presentation at work, I wanted to compare the performance of NodeJS to C. Here is what I wrote:

Node.js (for.js):

var d = 0.0,
    start = new Date().getTime();

for (var i = 0; i < 100000000; i++)
{
  d += i >> 1;
}

var end = new Date().getTime();

console.log(d);
console.log(end - start);

C (for.c)

#include <stdio.h>
#include <time.h>

int main () {
  clock_t start = clock();

  long d = 0.0;

  for (long i = 0; i < 100000000; i++)
  {
    d += i >> 1;    
  }

  clock_t end = clock();
  clock_t elapsed = (end - start) / (CLOCKS_PER_SEC / 1000); 

  printf("%ld\n", d);
  printf("%lu\n", elapsed);
}

Using GCC I compiled my for.c and ran it:

gcc for.c
./a.out

Results:

2499999950000000
198

Then I tried it in NodeJS:

node for.js

Results:

2499999950000000
116

After running numerous times, I discovered this held true no matter what. If I switched for.c to use a double instead of a long in the loop, the time C took was even longer!

Not trying to start a flame war, but why is Node.JS (116 ms.) so much faster than native C (198 ms.) at performing this same operation? Is Node.JS applying an optimization that GCC does not do out of the box?

EDIT:

Per suggestion in comments, I ran gcc -Wall -O2 for.c. Results improved to C taking 29 ms. This begs the question, how is it that the native C settings are not optimized as much as a Javascript compiler? Also, what is -Wall and -02 doing. I'm really curious about the details of what is going on here.

user4842163

This begs the question, how is it that the native C settings are not optimized as much as a Javascript compiler?

Since C is statically-compiled and linked, requiring a potentially lengthy build step of your entire codebase (I once worked in one that took almost an hour for a full optimized build, but only 10 minutes otherwise), and a very dangerous, hardware-level language that risks a lot of undefined behaviors if you don't treat it with care, the default settings of compilers usually don't optimize to smithereens since that's a developer/debug build intended to help with debugging and productivity with faster turnaround.

So in C you get a distinct separation between an unoptimized but faster-to-build, easier-to-debug developer/debug build and a very optimized, slower-to-build, harder-to-debug production/release build that runs really fast, and the default settings of compilers often favor the former.

With something like v8/NodeJS, you're dealing with a just-in-time compiler (dynamic compilation) that builds and optimizes only the necessary code on the fly at run-time. On top of that, JS is a much safer language and also often designed for security that doesn't allow you to work at the raw bits and bytes of the hardware.

As a result, it doesn't need that kind of strong release/debug build distinction of a native, statically-compiled language like C/C++. But it also doesn't let you put the pedal to the metal as you can in C if you really want.

A lot of people trying to benchmark C/C++ coming from other languages often fail to understand this build distinction and the importance of compiler/linker optimization settings and get confused. As you can see, with the proper settings, it's hard to beat the performance of these native compilers and languages that allow you to write really low-level code.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Why is my inclusive scan code 2x faster on CPU than on a GPU?

From Dev

x86_64: is IMUL faster than 2x SHL + 2x ADD?

From Dev

How to make videos play more than 2x faster

From Dev

Why is `-1 * x` faster than `-x` and why?

From Java

Why is 'x' in ('x',) faster than 'x' == 'x'?

From Dev

Why Windows download under VirtualBox is 20x faster than in native Linux?

From Dev

Why is Python faster than C++ in this case?

From Dev

Why is this simple loop faster in Go than in C?

From Dev

Why is Python sort faster than that of C++

From Java

Why is list(x for x in a) faster for a=[0] than for a=[]?

From Dev

Android: why is native code so much faster than Java code

From Java

Why is lodash.each faster than native forEach?

From Dev

Why in this case native promises seems to be faster than callbacks in chrome?

From Dev

Why is for loop on native python list faster than for loop on numpy array

From Dev

Why is Code 1 faster than Code 2?

From Java

Why is [] faster than list()?

From Dev

Why ReferenceEquals() is faster than ==

From Dev

Why is A* faster than Dijkstra

From Dev

why < is much faster than !=?

From Dev

Why my C# code is faster than my C code?

From Dev

Why is copying a file in C so much faster than C++?

From Dev

Why is this code so faster in java than in C++ and C#

From Dev

Why is C# running faster than C++?

From Dev

How come "median" is 2x faster than "mean" using statistics package?

From Java

Why is 2 * x * x faster than 2 * ( x * x ) in Python 3.x, for integers?

From Dev

Why is 2 * x * x faster than 2 * ( x * x ) in Python 3.x, for integers?

From Dev

benchmark of simple math functions: why is Fortran and Julia faster than C

From Java

C++: Why is unordered_set::find faster than find?

From Dev

Why is Matlab 11 times faster than C++

Related Related

  1. 1

    Why is my inclusive scan code 2x faster on CPU than on a GPU?

  2. 2

    x86_64: is IMUL faster than 2x SHL + 2x ADD?

  3. 3

    How to make videos play more than 2x faster

  4. 4

    Why is `-1 * x` faster than `-x` and why?

  5. 5

    Why is 'x' in ('x',) faster than 'x' == 'x'?

  6. 6

    Why Windows download under VirtualBox is 20x faster than in native Linux?

  7. 7

    Why is Python faster than C++ in this case?

  8. 8

    Why is this simple loop faster in Go than in C?

  9. 9

    Why is Python sort faster than that of C++

  10. 10

    Why is list(x for x in a) faster for a=[0] than for a=[]?

  11. 11

    Android: why is native code so much faster than Java code

  12. 12

    Why is lodash.each faster than native forEach?

  13. 13

    Why in this case native promises seems to be faster than callbacks in chrome?

  14. 14

    Why is for loop on native python list faster than for loop on numpy array

  15. 15

    Why is Code 1 faster than Code 2?

  16. 16

    Why is [] faster than list()?

  17. 17

    Why ReferenceEquals() is faster than ==

  18. 18

    Why is A* faster than Dijkstra

  19. 19

    why < is much faster than !=?

  20. 20

    Why my C# code is faster than my C code?

  21. 21

    Why is copying a file in C so much faster than C++?

  22. 22

    Why is this code so faster in java than in C++ and C#

  23. 23

    Why is C# running faster than C++?

  24. 24

    How come "median" is 2x faster than "mean" using statistics package?

  25. 25

    Why is 2 * x * x faster than 2 * ( x * x ) in Python 3.x, for integers?

  26. 26

    Why is 2 * x * x faster than 2 * ( x * x ) in Python 3.x, for integers?

  27. 27

    benchmark of simple math functions: why is Fortran and Julia faster than C

  28. 28

    C++: Why is unordered_set::find faster than find?

  29. 29

    Why is Matlab 11 times faster than C++

HotTag

Archive