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


当前回答

这是自动和手动的区别,高级语言是抽象的,因此是自动化的。C/ c++是人工控制和处理的,甚至错误检查代码有时也是人工劳动。

C和c++也是编译语言,这意味着没有任何一种语言可以在任何地方运行,这些语言必须针对您使用的硬件进行微调,从而增加了额外的隐患。尽管现在C/ c++编译器在所有平台上变得越来越普遍,这有点令人不安。您可以在平台之间进行交叉编译。这仍然不是一个到处运行的情况,你基本上是在指示编译器a针对编译器B编译相同的代码,不同的架构。

归根结底,C语言并不意味着容易理解或推理,这也是为什么它们被称为系统语言。他们出现在所有高层次抽象的废话之前。这也是为什么它们不用于前端web编程。他们只是不适合这项任务,他们的意思是解决传统语言工具无法解决的复杂问题。

这就是为什么你会得到一些疯狂的东西(微架构、驱动程序、量子物理、AAA游戏、操作系统),这些东西C和c++非常适合。速度和数据处理是主要领域。

其他回答

不要轻信别人的话,看看在代码的任何性能关键部分,C语言和你选择的语言的反汇编。我认为你可以在Visual Studio运行时的反汇编窗口中看到已分解的. net。如果Java使用windbg有点棘手,应该是可能的,尽管如果你用。net来做,许多问题将是相同的。

如果没有必要的话,我不喜欢用C来编写,但我认为,这些回答中吹捧除C之外其他语言的速度的许多主张可以放在一边,只需用C和您选择的高级语言分解相同的例程,特别是如果涉及大量数据(这在性能关键型应用程序中很常见)。Fortran在其专业领域可能是个例外,不知道。它比C高吗?

第一次比较JITed代码和本地代码时,我解决了。net代码是否能与C代码运行得相当的所有问题。额外的抽象层次和所有的安全检查都带来了巨大的成本。同样的成本可能也适用于Java,但不要相信我的话,在性能至关重要的地方尝试一下。(有没有人足够了解JITed Java来在内存中找到一个编译过的过程?这当然是可能的)

这实际上是一个长期存在的谎言。虽然C程序确实经常更快,但情况并非总是如此,特别是当C程序员不太擅长它的时候。

人们往往会忘记的一个明显的漏洞是,当程序必须为某种IO阻塞时,比如任何GUI程序中的用户输入。在这些情况下,使用什么语言并不重要,因为您受到数据传入速度的限制,而不是处理数据的速度。在这种情况下,不管你使用的是C、Java、c#甚至Perl;你不能比数据进入的速度更快。

The other major thing is that using garbage collection and not using proper pointers allows the virtual machine to make a number of optimizations not available in other languages. For instance, the JVM is capable of moving objects around on the heap to defragment it. This makes future allocations much faster since the next index can simply be used rather than looking it up in a table. Modern JVMs also don't have to actually deallocate memory; instead, they just move the live objects around when they GC and the spent memory from the dead objects is recovered essentially for free.

This also brings up an interesting point about C and even more so in C++. There is something of a design philosophy of "If you don't need it, you don't pay for it." The problem is that if you do want it, you end up paying through the nose for it. For instance, the vtable implementation in Java tends to be a lot better than C++ implementations, so virtual function calls are a lot faster. On the other hand, you have no choice but to use virtual functions in Java and they still cost something, but in programs that use a lot of virtual functions, the reduced cost adds up.

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

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

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

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

1)正如其他人所说,C为你做的更少。没有初始化变量,没有数组边界检查,没有内存管理等。其他语言中的这些特性会消耗C语言不需要的内存和CPU周期。

2) Answers saying that C is less abstracted and therefore faster are only half correct I think. Technically speaking, if you had a "sufficiently advanced compiler" for language X, then language X could approach or equal the speed of C. The difference with C is that since it maps so obviously (if you've taken an architecture course) and directly to assembly language that even a naive compiler can do a decent job. For something like Python, you need a very advanced compiler to predict the probable types of objects and generate machine code on the fly -- C's semantics are simple enough that a simple compiler can do well.

在过去,只有两种类型的语言:编译型和解释型。

编译语言利用“编译器”读取语言语法并将其转换为相同的汇编语言代码,这可以直接在CPU上进行。解释型语言使用了几种不同的方案,但从本质上讲,语言语法被转换成一种中间形式,然后在“解释器”(用于执行代码的环境)中运行。

因此,在某种意义上,在代码和机器之间存在另一个“层”——解释器。而且,在计算机中,越多就意味着使用更多的资源。翻译速度较慢,因为他们必须执行更多的操作。

More recently, we've seen more hybrid languages like Java, that employ both a compiler and an interpreter to make them work. It's complicated, but a JVM is faster, more sophisticated and way more optimized than the old interpreters, so it stands a much better change of performing (over time) closer to just straight compiled code. Of course, the newer compilers also have more fancy optimizing tricks so they tend to generate way better code than they used to as well. But most optimizations, most often (although not always) make some type of trade-off such that they are not always faster in all circumstances. Like everything else, nothing comes for free, so the optimizers must get their boast from somewhere (although often times it using compile-time CPU to save runtime CPU).

Getting back to C, it is a simple language, that can be compiled into fairly optimized assembly and then run directly on the target machine. In C, if you increment an integer, it's more than likely that it is only one assembler step in the CPU, in Java however, it could end up being a lot more than that (and could include a bit of garbage collection as well :-) C offers you an abstraction that is way closer to the machine (assembler is the closest), but you end up having to do way more work to get it going and it is not as protected, easy to use or error friendly. Most other languages give you a higher abstraction and take care of more of the underlying details for you, but in exchange for their advanced functionality they require more resources to run. As you generalize some solutions, you have to handle a broader range of computing, which often requires more resources.

保罗。