Original address: http://www.web-tinker.com/article/20374.html
Some time ago, an experiment was performed to test the execution times of 1E9 empty loops in Nodejs and C + +. So I and small friends instantly stunned, nodejs performance is to reverse the day? Although I've been complaining about the V8 engine in various nook and cranny of the optimization done is too disgusting, but in any case its high basic performance is not to be denied.
#include <Cstdio>
#include <CTime>
int Main(void){
int T=Clock();
for(int I=0;I<1E9;I++);
Printf("%d\n",Clock()-T);
return 0;
}
console.time("test");
for(var i=0;i<1E9;i++);
console.timeEnd("test");
Nodejs is to reverse the sky? Faster than C + +? This result is only because the compilation of C + + does not open the maximum optimization. When you turn on maximum optimization, you can get
So C + + is faster than Nodejs is sure, but from this result is not much faster. A lot of people are skeptical, there is only one empty statement in the test loop, is the V8 engine optimizing it out? But how did the 1000-millisecond time run out if it was optimized? Maybe this test doesn't explain the problem, so let's do a real-world test, sort an integer array of length 1E6. Here stole a lazy, C + + directly using the C standard library of the Qsort function, JavaScript used in the previous article in the sorting algorithm.#include <Cstdio>
#include <Cstdlib>
#include <CTime>
#define L 1E6
int Main(void){
int *A=New int[L];
for(int I=0;I<L;I++)A[I]=Rand();
int T=Clock();
Qsort(A,L,sizeof(int),[](Const void *A,Const void *B)->int{
return *(int*)A-*(int*)B;
});
Printf("%d\n",Clock()-T);
Delete A;
return 0;
}
functionSort (a) {
vars=[0, a.length-1],p=0, L,r,i,j,v;
while(s.length>p) {
L=i=s[p++],r=j=s[p++],v=a[i];
while(I<J) {
while(a[j]>=v&&i<j) j--;
A[I]=A[J];
while(a[i]<=v&&i<j) i++;
A[j]=a[i];
};
A[i]=v;
if(I-1>l) S.push (l,i-1);
if(i+1<r) S.push (i+1, R);
};
};
varS,i,t;
for(i=0,s=[];i<1E6; i++) S.push (Math. Random () *1E6|0);
t=NewDate;
Sort (s);
Console.log(NewDate-T);
C + + is faster than Nodejs, but performance comparison is usually measured by time-consuming order of magnitude, these two results are basically no difference after the logarithm, C + + really no faster than Nodejs. So, Nodejs's performance is very advantageous compared to a whole bunch of other web programming languages.
Performance comparison of Nodejs and C + + (RPM)