在听StackOverflow播客的时候,经常有人说“真正的程序员”是用C语言编写的,而C语言的速度要快得多,因为它“接近机器”。把前面的断言留到另一篇文章,C有什么特别之处,使它比其他语言更快?或者换句话说:什么能阻止其他语言编译成二进制代码,使其运行速度与C语言一样快?


当前回答

只需在您的IDE中逐步检查机器代码,您就会看到为什么它更快(如果它更快的话)。它省去了很多手把手的环节。很有可能你的Cxx也会被告知不要使用,在这种情况下,它应该是相同的。

编译器优化被高估了,就像几乎所有关于语言速度的看法一样。

优化生成的代码只会对热点代码产生影响,也就是说,没有函数调用(显式或隐式)的紧凑算法。在其他地方,收效甚微。

其他回答

我在链接上找到了一个关于为什么有些语言更快,有些更慢的答案,我希望这将更清楚为什么C或c++比其他语言更快,还有一些其他语言也比C更快,但我们不能使用所有的语言。一些解释-

Fortran仍然重要的一个重要原因是它的速度快:用Fortran编写的数字处理例程往往比用大多数其他语言编写的等效例程要快。在这个领域与Fortran竞争的语言是C和c++,因为它们在性能上具有竞争力。

这就提出了一个问题:为什么?是什么让c++和Fortran速度如此之快?为什么它们比其他流行语言(如Java或Python)性能更好?

解释与编译 根据编程语言所鼓励的编程风格和所提供的特性,有许多方法可以对编程语言进行分类和定义。在性能方面,最大的区别是解释语言和编译语言之间的区别。

划分并不难;而是有一个光谱。在一端,我们有传统的编译语言,包括Fortran、C和c++。在这些语言中,有一个独立的编译阶段,将程序的源代码转换为处理器可以使用的可执行形式。

这个编译过程有几个步骤。对源代码进行分析和解析。基本的编码错误,如错字和拼写错误,此时可以检测到。解析后的代码用于生成内存中的表示,该表示也可用于检测错误——这一次是语义错误,例如调用不存在的函数,或者试图对文本字符串执行算术操作。

然后,这个内存中表示形式用于驱动代码生成器,即生成可执行代码的部分。代码优化,以提高所生成代码的性能,在此过程中的不同时间执行:可以在代码表示上执行高级优化,而在代码生成器的输出上使用低级优化。

实际执行代码发生在后面。整个编译过程只是用来创建可以执行的内容。

在另一端,我们有口译员。解释器将包括一个类似于编译器的解析阶段,但这随后用于驱动直接执行,程序立即运行。

最简单的解释器包含与该语言支持的各种特性相对应的可执行代码,因此它将具有用于添加数字、连接字符串以及给定语言所具有的任何其他功能的函数。当它解析代码时,它将查找相应的函数并执行它。在程序中创建的变量将保存在某种将其名称映射到其数据的查找表中。

解释器风格的最极端的例子是类似批处理文件或shell脚本的东西。在这些语言中,可执行代码通常甚至不内置在解释器本身中,而是单独的独立程序。

So why does this make a difference to performance? In general, each layer of indirection reduces performance. For example, the fastest way to add two numbers is to have both of those numbers in registers in the processor, and to use the processor's add instruction. That's what compiled programs can do; they can put variables into registers and take advantage of processor instructions. But in interpreted programs, that same addition might require two lookups in a table of variables to fetch the values to add, then calling a function to perform the addition. That function may very well use the same processor instruction as the compiled program uses to perform the actual addition, but all the extra work before the instruction can actually be used makes things slower.

如果你想知道更多,请查看来源

使用现代优化编译器,纯C程序不太可能比编译后的。net代码快得多,如果有的话。通过像。net这样的框架为开发人员提供的生产力提高,您可以在一天内完成过去用普通c语言需要几周或几个月才能完成的工作。再加上与开发人员的工资相比,硬件成本低廉,用高级语言编写这些东西并以任何缓慢的速度抛出硬件要便宜得多。

The reason Jeff and Joel talk about C being the "real programmer" language is because there is no hand-holding in C. You must allocate your own memory, deallocate that memory, do your own bounds-checking, etc. There's no such thing as new object(); There's no garbage collection, classes, OOP, entity frameworks, LINQ, properties, attributes, fields, or anything like that. You have to know things like pointer arithmetic and how to dereference a pointer. And, for that matter, know and understand what a pointer is. You have to know what a stack frame is and what the instruction pointer is. You have to know the memory model of the CPU architecture you're working on. There is a lot of implicit understanding of the architecture of a microcomputer (usually the microcomputer you're working on) when programming in C that simply is not present nor necessary when programming in something like C# or Java. All of that information has been off-loaded to the compiler (or VM) programmer.

这不是语言的问题,而是工具和库的问题。C语言可用的库和编译器比新语言要老得多。你可能认为这会让它们变慢,但事实恰恰相反。

这些库是在处理能力和内存非常重要的时候编写的。它们必须写得非常高效,才能发挥作用。C编译器的开发人员也花了很长时间为不同的处理器进行各种巧妙的优化。C语言的成熟和广泛采用使得它比同时期的其他语言具有显著的优势。它还使C语言在速度上比那些不像C语言那样强调原始性能的新工具更有优势。

令人惊讶的是,“C/ c++必须比Java快,因为Java是解释性的”的神话仍然活跃。有几年前的文章,也有最近的文章,用概念或测量来解释为什么这种情况并不总是如此。

当前的虚拟机实现(顺便说一下,不仅仅是JVM)可以利用程序执行期间收集的信息来动态地优化代码,使用各种技术:

将频繁的方法呈现给机器代码, 内联小方法, 锁紧调整

以及各种其他调整,这些调整是基于了解代码实际在做什么,以及它运行的环境的实际特征。

For the most part, every C instruction corresponds to a very few assembler instructions. You are essentially writing higher level machine code, so you have control over almost everything the processor does. Many other compiled languages, such as C++, have a lot of simple looking instructions that can turn into much more code than you think it does (virtual functions, copy constructors, etc..) And interpreted languages like Java or Ruby have another layer of instructions that you never see - the Virtual Machine or Interpreter.