user2826084
user2826084

Reputation: 545

Node js comparison to C

On my dual core machine, Node JS runs faster than an equivalent program written in C.

Is node so well optimized that it actually is more efficient, or is there something wrong with my C program that makes it slower?

Node js code:

var Parallel = require("paralleljs"); 

function slow(n){
    var i = 0; 
    while(++i < n * n){}
    return i; 
}

var p = new Parallel([20001, 32311, 42222]);
p.map(slow).then(function(data){
    console.log("Done!"+data.toString()); 
});

C code:

#include <stdio.h>
#include <pthread.h>

struct thread_s {
    long int n; 
    long int r; 
}; 

void *slow(void *p){
    thread_s *t = (thread_s*)p; 
    long int i = 0; 
    while(++i < t->n * t->n){}
    t->r = i; 
    pthread_exit( 0 ); 
}

thread_s arr[] = {{20001, 0}, {32311, 0}, {42222, 0}};

int main(){
    pthread_t t[3]; 
    for(int c = 0; c < 3; c++){
        pthread_create(&t[c], NULL, slow, &arr[c]); 
    }
    for(int c = 0; c < 3; c++){
        pthread_join(t[c], NULL); 
    }
    printf("Done! %ld %ld %ld\n", arr[0].r, arr[1].r, arr[2].r); 
    return 0; 
}

Upvotes: 4

Views: 418

Answers (3)

hyde
hyde

Reputation: 62797

Question currently lacks details on benchmark, so it is impossible to say anything definitive about it. However, general comparison between V8 running javascript, and a bknary program compiled from C source is possible.

V8 is pretty darn good at JIT compilation, so while there is the overhead of JIT compilation, this compensates for dynamic nature of JavaScript, so for simple integer operations in a loop there's no reason for JIT code to be slower. JIT

Another consideration is startup time. If you load node.js first and load javascript from interactive prompt, startup time of script is minimal even with JIT, especially compared to dynamically linked binary which needs to resolve symbols etc. If you have small statically linked binary, it will start very fast, and will have done a lot of processing by the time a new node.js is even started and starts to look for some Javascript to execute. You need to be careful how you handle this in benchmarks, or results will be meaningless.

Upvotes: 0

Serhii Kuts
Serhii Kuts

Reputation: 434

All basic operations (+-, Math.xx etc.) are mapped to V8 engine which just execute it as C programm. So you should have pretty same results for C vs Node.js in these kind of scenarios.

Also I have tried C#.NET vs Node Fibonacci of 45. And first time I ran it was 5 times slower for C#, that was really strange. In a moment I understood that this is due to debug mode I ran C# app.

Going to release make it very close(20sec node, 22sec C#), probably this is just measurement inconsistency.

In any case this is just a matter of percents.

Upvotes: 0

usr
usr

Reputation: 171188

You are benchmarking a toy program which is not a good way to compare compilers. Also, the loops you are doing have no side-effects. All it does is set i to n * n. The loops should be optimized out. Are you running unoptimized?

Try to compute something real that approximates the work-load that you will later apply in production. If your code will be numerics-heavy you could benchmark a naive matrix multiplication for example.

Upvotes: 2

Related Questions