在听StackOverflow播客的时候,经常有人说“真正的程序员”是用C语言编写的,而C语言的速度要快得多,因为它“接近机器”。把前面的断言留到另一篇文章,C有什么特别之处,使它比其他语言更快?或者换句话说:什么能阻止其他语言编译成二进制代码,使其运行速度与C语言一样快?
当前回答
我猜你忘了汇编语言也是一种语言:)
但是说真的,只有当程序员知道自己在做什么的时候,C程序才会更快。你可以很容易地编写一个C程序,它比用其他语言编写的程序运行得更慢。
C语言之所以更快,是因为它就是这样设计的。它允许你做很多“低级”的事情,帮助编译器优化代码。或者,我们可以说,你程序员负责优化代码。但这通常相当棘手,而且容易出错。
其他语言,就像前面提到的其他语言一样,更关注程序员的生产力。人们普遍认为程序员的时间比机器的时间要昂贵得多(即使在过去)。因此,尽量减少程序员花在编写和调试程序上的时间,而不是减少程序的运行时间,是很有意义的。为了做到这一点,您将牺牲一些可以使程序更快的事情,因为许多事情都是自动化的。
其他回答
实际上,在某些应用程序(数字)中,甚至C也可以被击败,我指的不是汇编语言,而是老的、经常被嘲笑的Fortran。原因是,Fortran保证没有指针别名。
主要的因素是它是一种静态类型的语言,可以编译为机器代码。此外,由于它是一种低级语言,它通常不会做任何您不让它做的事情。
这些是我想到的其他一些因素。
Variables are not automatically initialized No bounds checking on arrays Unchecked pointer manipulation No integer overflow checking Statically-typed variables Function calls are static (unless you use function pointers) Compiler writers have had lots of time to improve the optimizing code. Also, people program in C for the purpose of getting the best performance, so there's pressure to optimize the code. Parts of the language specification are implementation-defined, so compilers are free to do things in the most optimal way
大多数静态类型语言的编译速度可以和C语言一样快,甚至比C语言更快,特别是如果它们可以假设C语言因为指针别名等原因而不能这样做的话。
这实际上是一个长期存在的谎言。虽然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一样快?
没什么。像Java或。net语言这样的现代语言更多地面向程序员的生产力,而不是性能。现在硬件很便宜。此外,编译到中间表示提供了很多好处,如安全性,可移植性等。net CLR可以利用不同的硬件-例如,你不需要手动优化/重新编译程序来使用SSE指令集。
我在链接上找到了一个关于为什么有些语言更快,有些更慢的答案,我希望这将更清楚为什么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.
如果你想知道更多,请查看来源