java 动态编译语言 vs 静态编译语言

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12600296/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 09:30:24  来源:igfitidea点击:

Dynamically compiled language vs statically compiled language

javaccompiler-constructioncompilationprogramming-languages

提问by Geek

The first line of this article by Brian Goetzmade me to post this question in SO . Here 's the line again:

Brian Goetz 撰写的这篇文章的第一行让我在 SO 中发布了这个问题。这里又是一行:

Writing and interpreting performance benchmarks for dynamically compiled languages, such as Java, is far more difficult than for statically compiled languages like C or C++.

为动态编译的语言(如 Java)编写和解释性能基准测试远比静态编译的语言(如 C 或 C++)困难得多。

I know the answer of statically typed vs dynamically typed language. But what is the difference between dynamically compiled language vs statically compiled language?

我知道静态类型与动态类型语言答案。但是动态编译语言与静态编译语言之间有什么区别?

回答by Philipp

Dynamic compiledand dynamically typeddon't have much to do with each other. Typing is part of the language syntax, while the compilation strategy is part of the implementation of the language.

动态编译和动态类型相互之间没有太大关系。打字是语言语法的一部分,而编译策略是语言实现的一部分。

Dynamic typing means that you don't necessarily have to declare the type when you declare a variable and that conversion between types happens automatically in most cases.

动态类型意味着您不必在声明变量时声明类型,并且在大多数情况下,类型之间的转换会自动发生。

Dynamic compiling means that the language is compiled to machine code while the program is being executed, not before. This allows, for example, just-in-time optimization - the code is optimized while the application is running. A JIT optimizer has the advantage that it has much more reliable information about which branches of the code are used most often and how they are usually used, because it can observe the application in action before applying optimizations.

动态编译意味着在程序执行时将语言编译为机器代码,而不是之前。例如,这允许实时优化 - 在应用程序运行时优化代码。JIT 优化器的优势在于它拥有关于代码的哪些分支最常使用以及它们通常如何使用的更可靠的信息,因为它可以在应用优化之前观察应用程序的运行情况。

Dynamic compilation is a problem for automatic benchmarking, because multiple measurements of the same program code section can compare completely different machine code interpretations because the optimizer has decided to change the implementation between two runs.

动态编译是自动基准测试的一个问题,因为对同一程序代码部分的多次测量可以比较完全不同的机器代码解释,因为优化器已决定在两次运行之间更改实现。

回答by Jesper

C and C++ source code are normally compiled to native machine code by a compiler.

C 和 C++ 源代码通常由编译器编译为本地机器代码。

Java is compiled to bytecode by the Java compiler. When you run your Java program, a just-in-time (JIT) compilermight compile the Java bytecode to native machine code for the CPU that the program is running on.

Java 由 Java 编译器编译为字节码。当您运行 Java 程序时,即时 (JIT) 编译器可能会将 Java 字节码编译为运行程序的 CPU 的本地机器代码。

Compiling the program to native machine code when the programs runs is also called dynamic compilation.

在程序运行时将程序编译为本机代码也称为动态编译

回答by HonkyTonk

Dynamic versus static compilation refers to how, and if, the compiler generated code can be changed during execution to change the performance or the program.

动态编译与静态编译是指如何以及是否可以在执行期间更改编译器生成的代码以更改性能或程序。

Static compilation allows no such manipulation since all addresses and jumps are fixed (unless you yourself write the code to change the instruction order during execution).

静态编译不允许这样的操作,因为所有地址和跳转都是固定的(除非您自己编写代码以在执行期间更改指令顺序)。

Dynamic compilation allows inspection during program execution and the gathered information can be used to make things run faster. The Wikipedia articleis an easy read and pretty informative.

动态编译允许在程序执行期间进行检查,收集的信息可用于使程序运行得更快。在维基百科的文章是很容易看懂和相当丰富。

回答by Alexei Kaigorodov

The difference, from the benchmark point of view, is that execution time of dynamically compiled program can change dramatically during the execution. Usually java code is being interpreted first, then, when interpreter discovers that some method are invoked many times, it calls JIT compiler to convert them to native code. The compiled code is still monitored and, when frequently executed parts of code ("hot spots") are determined, they are optimized further.

从基准的角度来看,不同之处在于动态编译程序的执行时间在执行过程中会发生巨大变化。通常先解释java代码,然后,当解释器发现某个方法被多次调用时,它会调用JIT编译器将它们转换为本地代码。编译后的代码仍会受到监控,并且在确定代码的频繁执行部分(“热点”)时,会进一步优化它们。

As a minimum, benchmarks for dynamically compiled languages must treat "warming phase" (when code is optimized) separately from the rest of the execution.

至少,动态编译语言的基准测试必须将“预热阶段”(代码优化时)与执行的其余部分分开处理。