在听StackOverflow播客的时候,经常有人说“真正的程序员”是用C语言编写的,而C语言的速度要快得多,因为它“接近机器”。把前面的断言留到另一篇文章,C有什么特别之处,使它比其他语言更快?或者换句话说:什么能阻止其他语言编译成二进制代码,使其运行速度与C语言一样快?
当前回答
只需在您的IDE中逐步检查机器代码,您就会看到为什么它更快(如果它更快的话)。它省去了很多手把手的环节。很有可能你的Cxx也会被告知不要使用,在这种情况下,它应该是相同的。
编译器优化被高估了,就像几乎所有关于语言速度的看法一样。
优化生成的代码只会对热点代码产生影响,也就是说,没有函数调用(显式或隐式)的紧凑算法。在其他地方,收效甚微。
其他回答
The lack of abstraction is what makes C faster. If you write an output statement you know exactly what is happening. If you write an output statement in java it is getting compiled to a class file which then gets run on a virtual machine introducing a layor of abstraction. The lack of object oriented features as a part of the language also increases it's speed do to less code being generated. If you use C as an object oriented language then you are doing all the coding for things such as classes, inharitence, etc. This means rather then make something generalized enough for everyone with the amount of code and the performance penelty that requires you only write what you need to get the job done.
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.
甚至C和c++之间的差异有时也会很大。
当你为一个对象分配内存,调用构造函数,在字边界上对齐内存等等,程序最终会经历很多开销,这些开销都是从程序员那里抽象出来的。
C迫使您查看程序所做的每一件事,通常是非常精细的细节。这使得编写执行大量与当前目标无关的任务的代码变得更加困难(尽管并非完全不可能)。
因此,例如在BASIC程序中,你可以使用INPUT关键字从STDIN读取字符串并自动为其变量分配内存,在C中,程序员通常已经分配了内存,并可以控制诸如程序是否阻塞I/O,以及它是否在获得所需信息后停止读取输入或继续读取字符到行尾等事情。
C also performs a lot less error-checking than other languages, presuming the programmer knows what they're doing. So whereas in PHP if you declare a string $myStr = getInput(); and go on to reference $myStr[20], but the input was only 10 characters long, PHP will catch this and safely return to you a blank string. C assumes that you've either allocated enough memory to hold data past the end of the string or that you know what information comes after the string and are trying to reference that instead. These small factors have a huge impact on overhead in aggregate.
我猜你忘了汇编语言也是一种语言:)
但是说真的,只有当程序员知道自己在做什么的时候,C程序才会更快。你可以很容易地编写一个C程序,它比用其他语言编写的程序运行得更慢。
C语言之所以更快,是因为它就是这样设计的。它允许你做很多“低级”的事情,帮助编译器优化代码。或者,我们可以说,你程序员负责优化代码。但这通常相当棘手,而且容易出错。
其他语言,就像前面提到的其他语言一样,更关注程序员的生产力。人们普遍认为程序员的时间比机器的时间要昂贵得多(即使在过去)。因此,尽量减少程序员花在编写和调试程序上的时间,而不是减少程序的运行时间,是很有意义的。为了做到这一点,您将牺牲一些可以使程序更快的事情,因为许多事情都是自动化的。
The fastest running code would be carefully hand crafted machine code. Assembler will be almost as good. Both are very low level and it takes a lot of writing code to do things. C is a little above assembler. You still have the ability to control things at a very low level in the actual machine, but there is enough abstraction make writing it faster and easier then assembler. Other languages such as C# and JAVA are even more abstract. While Assembler and machine code are called low level languages, C# and JAVA (and many others) are called high level languages. C is sometimes called a middle level language.
推荐文章
- 在SQL Server上使用varchar(MAX) vs TEXT
- c++中size_t和int的区别是什么?
- .toArray(new MyClass[0]) or .toArray(new MyClass[myList.size()])?
- 在C和c++中静态变量存储在哪里?
- 是什么导致JNI调用变慢?
- 检查属性是否有属性
- errno线程安全吗?
- 如何快速清除JavaScript对象?
- 如何在C程序中获取当前目录?
- 互斥实例/教程?
- 如何添加一个'或'条件在#ifdef
- extern关键字对C函数的影响
- Node.js vs .Net性能
- 如果使用if-return-return或if-else-return?
- 转换Python程序到C/ c++代码?