Javascript是C ++的4倍? [英] Javascript is 4 times faster than C++?

查看:210
本文介绍了Javascript是C ++的4倍?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很长一段时间,我认为C ++比javascript快。然而,今天我做了一个基准脚本来比较两种语言的浮点计算的速度,结果是惊人的!



Javascript几乎是c ++的4倍!



我让两种语言在我的i5-430M笔记本电脑上做同样的工作,执行 a = a + b 100000000次。 C ++大约需要410ms,而javascript只需要大约120ms。



我真的不知道为什么javascript在这种情况下运行得那么快。



我用于javascript的代码是(与nodejs一起运行):

 (function(){
var a = 3.1415926,b = 2.718;
var i,j,d1,d2;
for(j = 0; j <10; j ++){
d1 = new Date();
for(i = 0; i< 100000000; i ++){
a = a + b;
}
d2 = new Date();
console.log(Time Cost:+(d2.getTime() - d1.getTime())+ms);
}
console.log(a =+ a);
})();

c ++(由g ++编译)的代码是:

  #include< stdio.h> 
#include< ctime>

int main(){
double a = 3.1415926,b = 2.718;
int i,j;
clock_t start,end;
for(j = 0; j< 10; j ++){
start = clock();
for(i = 0; i <100000000; i ++){
a = a + b;
}
end = clock();
printf(Time Cost:%dms\\\
,(end-start)* 1000 / CLOCKS_PER_SEC);
}
printf(a =%lf \\\
,a);
return 0;
}


解决方案

如果您使用的是 Linux 系统(至少在这种情况下,它符合POSIX)。 clock() 调用返回消耗的时钟节拍数,并由 CLOCKS_PER_SEC 1,000,000 )缩放。



也就是说,如果您在这样的系统上,则是以微秒显示C和毫秒根据 JS在线文档)。所以,不是JS快了四倍,C ++实际上是250倍快。



现在可能是你在一个系统上 CLOCKS_PER_SECOND 是百万之外的其他程序,您可以在系统上运行以下程序,以查看它是否按相同的值缩放:

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

#define MILLION * 1000000

static void commaOut(int n,char c){
if(n< 1000){
printf %d%c,n,c);
return;
}

commaOut(n / 1000,',');
printf(%03d%c,n%1000,c);
}

int main(int argc,char * argv []){
int i;

系统(date);
clock_t start = clock();
clock_t end = start;

while(end-start< 300万){
for(i = 10 MILLION; i> 0; i--){};
end = clock();
}

系统(date);
commaOut(end - start,'\\\
');

return 0;
}

我的盒子上的输出是:

  11月17日星期二11:53:01 AWST 2015 
11月17日星期二11:53:31 AWST 2015
30,001,946

显示缩放因子为百万。如果您运行该程序或调查 CLOCKS_PER_SEC ,并且不是一个100万的缩放因子,您需要查看其他一些事情。 p>




第一步是确保你的代码实际上是由编译器优化的。这意味着,例如,为 gcc 设置 -O2 -O3 $ c>。



在未优化代码的系统上,我看到:

 时间成本:320ms 
时间成本:300ms
时间成本:300ms
时间成本:300ms
时间成本:300ms
时间成本:300ms
时间成本:300ms
时间成本:300ms
时间成本:300ms
时间成本:300ms
a = 2717999973.760710

它的速度是 -O2 的三倍,尽管答案略有不同,但只有百分之一a百分比:

 时间成本:140ms 
时间成本:110ms
时间成本:100ms
时间成本:100ms
时间成本:100ms
时间成本:100ms
时间成本:100ms
时间成本:100ms
时间成本:100ms
成本:100ms
a = 2718000003.159864



现代JavaScript引擎(V8,Rhino)是一种新的JavaScript引擎, ,etc)可以将代码编译为中间形式(甚至是机器语言),这可以允许性能与编译语言(如C)大致相等。



但是, ,你不倾向于选择JavaScript或C ++的速度,你选择他们的力量的领域。没有很多C编译器在浏览器内部浮动,我没有注意到许多操作系统和用JavaScript编写的嵌入式应用程序。


For a long time, I have thought of C++ as faster than javascript. However, today I made a benchmark script to compare the speed of floating point calculations in the two languages and the result is amazing!

Javascript is almost 4 times faster than c++!

I let both of the languages to do the same job on my i5-430M laptop, performing a = a + b for 100000000 times. C++ takes about 410 ms, while javascript takes only about 120 ms.

I really do not have any idea why javascript can run so fast in this case. Can anyone explain that?

The code I used for the javascript is (run with nodejs):

(function() {
    var a = 3.1415926, b = 2.718;
    var i, j, d1, d2;
    for(j=0; j<10; j++) {
        d1 = new Date();
        for(i=0; i<100000000; i++) {
            a = a + b;
        }
        d2 = new Date();
        console.log("Time Cost:" + (d2.getTime() - d1.getTime()) + "ms");
    }
    console.log("a = " + a);
})();

And the code for c++ (compiled by g++) is:

#include <stdio.h>
#include <ctime>

int main() {
    double a = 3.1415926, b = 2.718;
    int i, j;
    clock_t start, end;
    for(j=0; j<10; j++) {
        start = clock();
        for(i=0; i<100000000; i++) {
            a = a + b;
        }
        end = clock();
        printf("Time Cost: %dms\n", (end - start) * 1000 / CLOCKS_PER_SEC);
    }
    printf("a = %lf\n", a);
    return 0;
}

解决方案

I may have some bad news for you if you're on a Linux system (which complies with POSIX at least in this situation). The clock() call returns number of clock ticks consumed by the program and scaled by CLOCKS_PER_SEC, which is 1,000,000.

That means, if you're on such a system, you're talking in microseconds for C and milliseconds for JavaScript (as per the JS online docs). So, rather than JS being four times faster, C++ is actually 250 times faster.

Now it may be that you're on a system where CLOCKS_PER_SECOND is something other than a million, you can run the following program on your system to see if it's scaled by the same value:

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

#define MILLION * 1000000

static void commaOut (int n, char c) {
    if (n < 1000) {
        printf ("%d%c", n, c);
        return;
    }

    commaOut (n / 1000, ',');
    printf ("%03d%c", n % 1000, c);
}

int main (int argc, char *argv[]) {
    int i;

    system("date");
    clock_t start = clock();
    clock_t end = start;

    while (end - start < 30 MILLION) {
        for (i = 10 MILLION; i > 0; i--) {};
        end = clock();
    }

    system("date");
    commaOut (end - start, '\n');

    return 0;
}

The output on my box is:

Tuesday 17 November  11:53:01 AWST 2015
Tuesday 17 November  11:53:31 AWST 2015
30,001,946

showing that the scaling factor is a million. If you run that program, or investigate CLOCKS_PER_SEC and it's not a scaling factor of one million, you need to look at some other things.


The first step is to ensure your code is actually being optimised by the compiler. That means, for example, setting -O2 or -O3 for gcc.

On my system with unoptimised code, I see:

Time Cost: 320ms
Time Cost: 300ms
Time Cost: 300ms
Time Cost: 300ms
Time Cost: 300ms
Time Cost: 300ms
Time Cost: 300ms
Time Cost: 300ms
Time Cost: 300ms
Time Cost: 300ms
a = 2717999973.760710

and it's three times faster with -O2, albeit with a slightly different answer, though only by about one millionth of a percent:

Time Cost: 140ms
Time Cost: 110ms
Time Cost: 100ms
Time Cost: 100ms
Time Cost: 100ms
Time Cost: 100ms
Time Cost: 100ms
Time Cost: 100ms
Time Cost: 100ms
Time Cost: 100ms
a = 2718000003.159864

That would bring the two situations back on par with each other, something I'd expect since JavaScript is not some interpreted beast like in the old days, where each token is interpreted whenever it's seen.

Modern JavaScript engines (V8, Rhino, etc) can compile the code to an intermediate form (or even to machine language) which may allow performance roughly equal with compiled languages like C.

But, to be honest, you don't tend to choose JavaScript or C++ for its speed, you choose them for their areas of strength. There aren't many C compilers floating around inside browsers and I've not noticed many operating systems nor embedded apps written in JavaScript.

这篇关于Javascript是C ++的4倍?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆