为什么这个NodeJS 2x比本机C快? [英] Why is this NodeJS 2x faster than native C?

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

问题描述

为了便于演示,我想比较NodeJS和C的性能.这是我写的:

For the sake of a presentation at work, I wanted to compare the performance of NodeJS to C. Here is what I wrote:

Node.js(for.js):

Node.js (for.js):

var d = 0.0,
    start = new Date().getTime();

for (var i = 0; i < 100000000; i++)
{
  d += i >> 1;
}

var end = new Date().getTime();

console.log(d);
console.log(end - start);

C(for.c)

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

int main () {
  clock_t start = clock();

  long d = 0.0;

  for (long i = 0; i < 100000000; i++)
  {
    d += i >> 1;    
  }

  clock_t end = clock();
  clock_t elapsed = (end - start) / (CLOCKS_PER_SEC / 1000); 

  printf("%ld\n", d);
  printf("%lu\n", elapsed);
}

使用GCC,我编译了for.c并运行了它:

Using GCC I compiled my for.c and ran it:

gcc for.c
./a.out

结果:

2499999950000000
198

然后我在NodeJS中尝试过:

Then I tried it in NodeJS:

node for.js

结果:

2499999950000000
116

运行了无数次之后,我发现无论如何它都是正确的.如果我将for.c切换为在循环中使用double而不是long,则C花费的时间甚至更长!

After running numerous times, I discovered this held true no matter what. If I switched for.c to use a double instead of a long in the loop, the time C took was even longer!

不尝试发动火焰战争,但是为什么执行同一操作时Node.JS(116毫秒)比本机C(198毫秒)那么快? Node.JS是否正在应用GCC不能立即使用的优化?

Not trying to start a flame war, but why is Node.JS (116 ms.) so much faster than native C (198 ms.) at performing this same operation? Is Node.JS applying an optimization that GCC does not do out of the box?

根据评论中的建议,我运行了gcc -Wall -O2 for.c.结果提高到C耗时29 ms.这就引出了一个问题,即本机C设置没有像Javascript编译器那样优化程度如何?另外,-Wall和-02在做什么.我真的很好奇这里发生的事情.

Per suggestion in comments, I ran gcc -Wall -O2 for.c. Results improved to C taking 29 ms. This begs the question, how is it that the native C settings are not optimized as much as a Javascript compiler? Also, what is -Wall and -02 doing. I'm really curious about the details of what is going on here.

推荐答案

这引起了一个问题,即本机C设置没有像Javascript编译器那样优化吗?

由于C是静态编译和链接的,因此可能需要较长的整个代码库构建步骤(我曾经在一个完整的优化构建中花了将近一个小时的时间,否则只花了10分钟),这非常危险,如果您不慎重对待,它会冒很多不确定行为的风险,因此默认情况下,编译器的默认设置通常不会针对smithereens进行优化,因为这是开发人员/调试版本,旨在帮助您更快地进行调试和提高工作效率周转.

Since C is statically-compiled and linked, requiring a potentially lengthy build step of your entire codebase (I once worked in one that took almost an hour for a full optimized build, but only 10 minutes otherwise), and a very dangerous, hardware-level language that risks a lot of undefined behaviors if you don't treat it with care, the default settings of compilers usually don't optimize to smithereens since that's a developer/debug build intended to help with debugging and productivity with faster turnaround.

因此,在C语言中,您可以将未优化但构建速度更快,易于调试的 developer/debug 构建与非常优化,构建速度较慢,难于实现的开发人员区分开来.调试运行速度非常快的 production/release 构建,并且编译器的默认设置通常倾向于前者.

So in C you get a distinct separation between an unoptimized but faster-to-build, easier-to-debug developer/debug build and a very optimized, slower-to-build, harder-to-debug production/release build that runs really fast, and the default settings of compilers often favor the former.

使用v8/NodeJS之类的东西,您需要处理的是实时编译器(动态编译),该编译器仅在运行时动态地构建和优化所需的代码.最重要的是,JS是一种更加安全的语言,并且通常还为安全性而设计,不允许您在硬件的原始位和字节上工作.

With something like v8/NodeJS, you're dealing with a just-in-time compiler (dynamic compilation) that builds and optimizes only the necessary code on the fly at run-time. On top of that, JS is a much safer language and also often designed for security that doesn't allow you to work at the raw bits and bytes of the hardware.

因此,它不需要像C/C ++这样的本机静态编译语言的那种强大的发行/调试版本区别.但是,如果您真的想要的话,它也不允许您像在C语言中那样将踏板踩到金属上.

As a result, it doesn't need that kind of strong release/debug build distinction of a native, statically-compiled language like C/C++. But it also doesn't let you put the pedal to the metal as you can in C if you really want.

许多尝试对来自其他语言的C/C ++进行基准测试的人常常无法理解这种构造区别以及编译器/链接器优化设置的重要性,并感到困惑.如您所见,通过适当的设置,很难击败这些本机编译器和允许您编写真正的低级代码的语言的性能.

A lot of people trying to benchmark C/C++ coming from other languages often fail to understand this build distinction and the importance of compiler/linker optimization settings and get confused. As you can see, with the proper settings, it's hard to beat the performance of these native compilers and languages that allow you to write really low-level code.

这篇关于为什么这个NodeJS 2x比本机C快?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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