When I compile your code I get:
Code: Select all
$ gcc -o junk junk.c
$ time ./junk
real 0m8.967s
user 0m8.813s
sys 0m0.031s
Code: Select all
$ time node junk.js
real 0m1.818s
user 0m1.719s
sys 0m0.109s
Code: Select all
$ gcc -O3 -o junk junk.c
$ time ./junk
real 0m0.016s
user 0m0.000s
sys 0m0.000s
Well not really. Your code has no output. Turning on optimization actually removes the whole redundant loop and the executable just exits doing nothing!
OK, let's print the final count such that there is some output and the program has work to do. Like so:
Code: Select all
$ cat junk.c
#include <stdio.h>
#include <stdint.h>
#include <limits.h>
int main()
{
int max = 2147483637;
int count = 0;
while (count < max)
{
count += 10;
count -= 9;
}
printf("Count = %d\n", count);
return 0;
}
$ gcc -O3 -o junk junk.c
$ time ./junk
Count = 2147483637
real 0m0.019s
user 0m0.000s
sys 0m0.000s
No so. The optimizer is not stupid. It knows what that simple loop does, calculates the result at compile time and just prints that constant in the executable.
We can fix that my making count "volatile":
Code: Select all
$ cat junk.c
#include <stdio.h>
#include <stdint.h>
#include <limits.h>
volatile int count = 0;
int main()
{
int max = 2147483637;
while (count < max)
{
count += 10;
count -= 9;
}
printf("Count = %d\n", count);
return 0;
}
$ gcc -O3 -o junk junk.c
$ time ./junk
Count = 2147483637
real 0m8.563s
user 0m8.453s
sys 0m0.000s
From this simple test of yours I conclude that Javascript under node.js is 4.7 times faster than C.
How about that?!
All done on an MS Surface Pro 4, sorry no Pi to hand to try it there.