简体   繁体   English

编程语言的速度取决于编译器吗?

[英]Does the speed of programming language depend on compiler?

I am trying to learn how computer programs work and have this question. 我正在尝试学习计算机程序是如何工作的并且有这个问题。 I often read articles like "C/C++ are faster than java" or "Java and C#: speed comparison". 我经常阅读诸如“ C / C ++比Java更快”或“ Java和C#:速度比较”之类的文章。 In all cases programs written in any language is translated to assembly language. 在所有情况下,以任何语言编写的程序都将翻译为汇编语言。 So, what is the reason of speed differences in those languages. 那么,这些语言中速度差异的原因是什么。 Does that mean, the compiler of one language generates better and faster assembly code ? 这是否意味着一种语言的编译器会生成更好,更快的汇编代码?

Sort of. 有点。

There are several reasons why speeds will differ between compilers/interpreters/programming languages, some of it having to do with the compiler, and some of it having to do with the language itself. 编译器/解释器/编程语言之间的速度会有所不同的原因有很多,其中一些与编译器有关,而某些与语言本身有关。

Some programming languages require more overhead. 一些编程语言需要更多的开销。

If your language is very high level, it'll have more overhead compared to C, which is very low level. 如果您的语言非常高级,那么与C(非常低级)相比,它将有更多开销。 (garbage collection is a good example of this). (垃圾回收就是一个很好的例子)。 It becomes a tradeoff. 这成为一种折衷。 Do I want blazing fast binaries, or do I want to be able to write programs easily? 我想快速开发二进制文件,还是想轻松编写程序?

Languages are designed to do different things. 语言被设计用来做不同的事情。

For example, PHP is designed to be used on web servers, and nobody in their right mind would try and use it to create a top-tier fps game. 例如,PHP被设计为可在Web服务器上使用,并且没有任何明智的人会尝试并使用它来创建顶级fps游戏。 Different languages are better suited for different tasks, and will be faster in some areas then in others. 不同的语言更适合于不同的任务,并且在某些领域会比其他领域更快。

Not all languages compile to assembly. 并非所有语言都可以汇编。

While C/C++ may compile to assembly, languages like Java instead compile to bytecode and is run against the java virtual machine, for interoperability reasons. 尽管C / C ++可以编译为汇编语言,但出于互操作性的原因,像Java这样的语言却可以编译为字节码,并且可以针对Java虚拟机运行。 Once again, this is a tradeoff -- you gain portability at the expense of overhead. 再次,这是一个折衷方案-您以牺牲开销为代价获得了可移植性。

Furthermore, C/C++ doesn't even have to compile to assembly. 此外,C / C ++甚至不必编译即可汇编。 For example, enscriptem ultimately will compile C/C++ to Javascript so it can run on web browsers. 例如,enscriptem最终会将C / C ++编译为Javascript,以便它可以在Web浏览器上运行。

Compilers are not magic. 编译器不是魔术。

They're programs, and like all programs, have bugs and will improve (or degrade) over time. 它们是程序,并且像所有程序一样,都有错误,并且会随着时间的推移而改善(或降低)。 I could try writing a C compiler over the weekend, and I'd bet a million dollars that it would perform several orders of magnitude worse then a compiler/interpreter for the slowest language you can think of. 我可以尝试在周末编写C编译器,我敢打赌一百万美元,它会比您能想到的最慢的语言的编译器/解释器执行的性能差几个数量级。

Compiler/interpreter optimization is an ongoing field of research and study. 编译器/解释器的优化是一个正在进行的研究领域。

Every year, researchers are writing and publishing papers on a new way to compile and make programs run faster. 每年,研究人员都以一种新的方式来撰写和发表论文,以使程序编译并使程序运行更快。 If a language is newer, it may not yet have had the time to fully apply every optimization available. 如果语言是较新的,则它可能还没有时间完全应用所有可用的优化方法。 (see above). (往上看)。 Some optimizations may apply to only one kind of compiler/interpreter. 某些优化可能仅适用于一种编译器/解释器。


So, to summarize, the speed of a language is a mixture of the intrinsic features of the language itself, along with the maturity of the compiler/interpreter/platform used. 因此,总而言之,语言的速度是语言本身的内在特征与所使用的编译器/解释器/平台的成熟度的混合。

Compilers and interpreters are not some monolithic magical process that's constant between all programming languages -- they're all different, have different benefits and disadvantages, and are constantly in a state of flux. 编译器和解释器并不是在所有编程语言之间都保持不变的整体魔术过程-它们都是不同的,具有不同的优缺点,并且始终处于不断变化的状态。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 编程语言的速度对Web应用程序是否重要? - Does the speed of a programming language matter for web applications? 数据库查询和插入速度取决于什么? - What does database query and insert speed depend on? 查询的速度是否取决于表中的行数? - Does the speed of the query depend on the number of rows in the table? Internet请求速度与HTTP请求的编程语言速度? - Internet connection speed vs. Programming language speed for HTTP Requests? SQL Server查询性能是否取决于到客户端的网络速度? - does SQL server query performance depend on the speed of network to the client? 提高 Wordle Bot 的速度(适用于任何编程语言) - Increase Speed of Wordle Bot (General For Any Programming Language) 在大多数现代 64 位处理器上,“mulq”的速度是否取决于操作数? - On most modern 64-bit processors, does the speed of `mulq` depend on the operands? C / C ++ Windows控制台应用程序的速度取决于目标是32位还是64位? - Does speed of an C/C++, Windows console application depend on whether the target is 32 or 64 bit? 功能编程是否允许更好的运行时编译器优化? - Does Functional programming allow better runtime compiler optimizations? 网络编程语言基准? - web programming language benchmarks?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM