January 28, 2020

bug nodejs is slow

For testing out the performance of node.js a simple prime number generator was programmed in Javascript and in C as well. The node.js version has reached the half of the speed of the C version. During runtime, the cpu was utilized by 100%. The problem is, that node.js is not a native compiler but was written in C++ which is interpreting the Javascript code during runtime. The result is, that node.js is not able to reach the same performance like C++. For high performance applications the language is not fast enough.

/*
gcc -O2 prime.c
time ./a.out > out.txt
real 0m14.145s
user 0m14.120s
sys 0m0.006s

*/

#include stdio.h
int min=2, max=500000;
int main() {
  int flag;
  for (int i=min;i
    flag=0;
    for (int a=2;a<=i/2;a++) {
      if (i%a==0) { 
        flag=1;
        break;
      }
    }
    if (flag==0) printf("%d\n",i);
  }
  return 0;
}
/*
time node prime.js > out.txt
real 0m26.749s
user 0m26.641s
sys 0m0.127s
*/

var min=2, max=500000
for(var i=min;i
  var flag=0
  for(var a=2;a<=Math.trunc(i/2);a++) {
    if (i%a==0) { 
      flag=1
      break
    }
  }
  if(flag==0) { 
    console.log(i)
  }
}

No comments:

Post a Comment