为什么JavaScript看起来比C ++快4倍? [英] Why does JavaScript appear to be 4 times faster than C++?

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

问题描述

很长一段时间以来,我一直认为C ++比JavaScript更快.但是,今天,我制作了一个基准脚本来比较两种语言中浮点计算的速度,结果是惊人的!

For a long time, I had thought of C++ being 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似乎比C ++快四倍!

JavaScript appears to be almost 4 times faster than C++!

我让两种语言在i5-430M笔记本电脑上执行相同的工作,执行a = a + b 100000000次. C ++大约需要410毫秒,而JavaScript大约只需要120毫秒.

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.

我真的不知道为什么在这种情况下JavaScript运行如此之快.谁能解释一下?

I really do not have any idea why JavaScript runs so fast in this case. Can anyone explain that?

我用于JavaScript的代码是(与Node.js一起运行):

The code I used for the JavaScript is (run with Node.js):

(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 ++编译)的代码为:

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;
}

推荐答案

如果您使用的是 Linux 系统(至少在这种情况下符合POSIX),我可能会对您有一些坏消息). clock() 调用返回程序消耗并由CLOCKS_PER_SEC缩放的时钟滴答数,即1,000,000.

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.

这意味着,如果您在这样的系统上运行 ,则对于C语言而言,您说的是 microseconds ,对于JavaScript语言而言,您是指 ms 根据 JS在线文档).因此,C ++实际上不是JS快四倍,而是C ++快了250倍.

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.

现在可能是因为您所在的系统上的CLOCKS_PER_SECOND不是一百万,您可以在系统上运行以下程序以查看其是否按相同的值进行缩放:

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;
}

我的盒子上的输出是:

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

显示缩放比例为一百万.如果您运行该程序或调查CLOCKS_PER_SEC而不是 缩放因子为一百万,则需要查看其他内容.

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.

第一步是确保编译器实际上在优化您的代码.例如,这意味着将gcc设置为-O2-O3.

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

,使用-O2的速度要快三倍,尽管答案略有不同,尽管只有百分之一的百分之一:

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

这会使这两种情况彼此重合,这是我所期望的,因为JavaScript并不是像过去那样的可解释的野兽,每当看到令牌时,它们都将被解释.

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.

现代JavaScript引擎(V8,Rhino等)可以将代码编译为中间形式(甚至机器语言),从而可以使性能与C之类的编译语言大致相同.

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.

但是,说实话,您并不是倾向于选择JavaScript或C ++来提高速度,而是要根据它们的优势来选择它们.浏览器内部没有很多C编译器,而且我也没有注意到很多操作系统或用JavaScript编写的嵌入式应用.

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天全站免登陆