Java 即时编译器和解释器之间有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2426091/
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-08-13 07:27:42  来源:igfitidea点击:

What are the differences between a Just-in-Time-Compiler and an Interpreter?

java.netcompiler-constructioninterpreterjit

提问by Rookian

What are the differences between a Just-in-Time-Compiler and an Interpreter, and are there differences between the .NET and the Java JIT compiler?

即时编译器和解释器之间有什么区别,.NET 和 Java JIT 编译器之间有什么区别?

采纳答案by Mark Byers

Just-in-time compilationis the conversion of non-native code, for example bytecode, into native code just before it is executed.

即时编译是将非本地代码(例如字节码)在执行之前转换为本地代码。

From Wikipedia:

来自维基百科:

JIT builds upon two earlier ideas in run-time environments: bytecode compilation and dynamic compilation. It converts code at runtime prior to executing it natively, for example bytecode into native machine code.

JIT 建立在运行时环境中的两个早期思想之上:字节码编译和动态编译。它在本地执行之前在运行时将代码转换为本地机器代码,例如字节码。

An interpreterexecutes a program. It may or may not have a jitter.

一种解释执行程序。它可能有也可能没有抖动。

Again, from Wikipedia:

再次,来自维基百科:

An interpreter may be a program that either

  1. executes the source code directly
  2. translates source code into some efficient intermediate representation (code) and immediately executes this
  3. explicitly executes stored precompiled code made by a compiler which is part of the interpreter system

解释器可以是一个程序

  1. 直接执行源码
  2. 将源代码翻译成一些有效的中间表示(代码)并立即执行
  3. 显式执行由编译器生成的存储预编译代码,编译器是解释器系统的一部分

Both the standard Java and .NET distributions have JIT compilation, but it is not required by the standards. The JIT compiler in .NET and C# are of course different because the intermediate bytecode is different. The principle is the same though.

标准 Java 和 .NET 发行版都有 JIT 编译,但标准并不要求这样做。.NET 和 C# 中的 JIT 编译器当然不同,因为中间字节码不同。不过原理是一样的。

回答by crowne

An interpreter generates and executes machine code instructions on the fly for each instruction, regardless of whether it has previously been executed.
A JIT caches the instructions that have been previously interpreted to machine code, and reuses those native machine code instructions thus saving time & resources by not having to re-interpret statements that have already been interpreted.

解释器会为每条指令即时生成并执行机器代码指令,而不管它之前是否已执行过。
JIT 缓存先前已解释为机器代码的指令,并重用这些本地机器代码指令,从而无需重新解释已解释的语句,从而节省时间和资源。

回答by Kimi

When you compile a Microsoft.NET language, the complier generates code written in the Microsoft Intermediate Language (MSIL). MSIL is a set of instructions that can quickly be translated into native code.

编译 Microsoft.NET 语言时,编译器会生成以 Microsoft 中间语言 (MSIL) 编写的代码。MSIL 是一组可以快速转换为本机代码的指令。

A Microsoft.NET application can be run only after the MSIL code is translated into native machine code. In .NET Framework, the intermediate language is complied "just in time" (JIT) into native code when the application or component is run instead of compiling the application at development time.

Microsoft.NET 应用程序只能在 MSIL 代码转换为本机代码后才能运行。在 .NET Framework 中,当应用程序或组件运行时,中间语言被“及时”(JIT) 编译为本机代码,而不是在开发时编译应用程序。

more info

更多信息

回答by erasmus

JIT compiler produces binary machine codes translating block source code. Interpreter translates line by line.

JIT 编译器生成二进制机器代码来翻译块源代码。口译员逐行翻译。

回答by supercat

The question of whether an execution engine is a compiler or an interpreter can be answered very simply by considering what happens if a routine is executed 1,000 times. If code within the execution engine will have to examine some particular representation of the code 1,000 times, the execution engine is an interpreter of that representation. If code within the execution the execution engine will only have to examine that particular representation of the code some smaller number of times (typically, though not necessarily, once), it is a compiler or translator of that representation. Note that it is very common for an execution engine to take input code and convert it to some other form which can be examined more readily. Such an execution engine would combine a compiler or translator of the former form with an interpreter of the latter form.

执行引擎是编译器还是解释器的问题可以通过考虑如果例程执行 1,000 次会发生什么来非常简单地回答。如果执行引擎中的代码必须检查代码的某个特定表示 1,000 次,则执行引擎就是该表示的解释器。如果执行中的代码执行引擎将只需要检查代码的特定表示次数较少(通常但不一定是一次),则它是该表示的编译器或翻译器。请注意,执行引擎获取输入代码并将其转换为更容易检查的其他形式是很常见的。这种执行引擎会将前一种形式的编译器或翻译器与后一种形式的解释器结合起来。

Note that interpreters very seldom produce any form of machine code. Just about the only time an interpreter will produce machine code is when a statement is supposed to perform some operation that really cannot be done any other way. For example, if a BASIC interpreter running on the 8080 encounters the instruction "OUT 100,5", it would typically perform that operation by storing D3 64 C9 (OUT 64h / RET) into three consecutive bytes at some fixed address, loading A with 5, and CALLing that address. The interpreter may technically be generating machine code, but if one were to perform the same OUT instruction 500 times, the interpreter would have to re-generate the machine code every time.

请注意,解释器很少产生任何形式的机器代码。解释器产生机器码的唯一时间是当一个语句应该执行一些其他方式无法完成的操作时。例如,如果在 8080 上运行的 BASIC 解释器遇到指令“OUT 100,5”,它通常会通过将 D3 64 C9 (OUT 64h / RET) 存储到某个固定地址的三个连续字节中来执行该操作,加载 A 5,并呼叫该地址。从技术上讲,解释器可能会生成机器代码,但如果要执行 500 次相同的 OUT 指令,解释器每次都必须重新生成机器代码。

回答by bitan

When the first time a class is referenced the JIT Execution Engine re-compiles the .class files (primary Binaries) generated by Java Compiler containing JVM Instruction Set to Binaries containing HOST system's Instruction Set. JIT stores and reuses those recompiled binaries from Memory going forward, there by reducing interpretation time and benefits from Native code execution.

当第一次引用一个类时,JIT 执行引擎将包含 JVM 指令集的 Java 编译器生成的 .class 文件(主要二进制文件)重新编译为包含 HOST 系统指令集的二进制文件。JIT 存储和重用那些从内存中重新编译的二进制文件,从而减少解释时间并从本地代码执行中受益。

On the other hand a plain old java interpreter interprets one JVM instruction from class file at a time and calls a procedure against it.

另一方面,一个普通的旧 Java 解释器一次从类文件中解释一个 JVM 指令,并针对它调用一个过程。

Find a detail comparison here http://bitshub.blogspot.com/2010/01/Flavors-of-JVM.html

在此处找到详细比较http://bitshub.blogspot.com/2010/01/Flavors-of-JVM.html

回答by Colorfully Monochrome

I've always found that a more abstract explanation sometimes helps. Let's say that you are trying to ask everyone in Mexico "Hello. How are you?" (your source language) Of course, you'll first need to translate it to Spanish (the native languageof the country). That translation would be "Hola. Como estas?"

我一直发现更抽象的解释有时会有所帮助。假设您试图向墨西哥的每个人问“你好。你好吗?” (您的源语言)当然,您首先需要将其翻译成西班牙语(该国的母语)。那个翻译是“Hola. Como estas?”

If you know Spanish, there would be no need for you to translate (native code / assembler). You just ask "Hola. Como estas?"

如果您会西班牙语,则无需翻译(本机代码/汇编程序)。你只是问“你好。Como estas?”

If you don't know Spanish, there are 3 ways to deal with it.

如果您不会西班牙语,有 3 种方法可以解决。

The first is to get a Spanish Dictionary (a compiler) and look up what the Spanish words are before you go. Perhaps you realize that "Hola. Que tal?" is one syllable shorter (compiler optimization) and use that instead. This is language compilation; you are converting the information to the native language beforehand.

首先是获得一本西班牙语词典(一个编译器)并在你去之前查一下西班牙语单词是什么。也许你意识到“Hola. Que tal?” 是一个更短的音节(编译器优化)并改用它。这是语言编译;您事先将信息转换为母语。

The second is where you look up the words in the Spanish Dictionary while you are standing in front of the first person and then store the result (looking up the words just-in-time). The advantage here is that you could get a Mandarin Dictionary and then do the same experiment in China without having to keep ten sticky notes (binaries for different platforms) of translated phrases.

第二个是当您站在第一个人面前时在西班牙语词典中查找单词,然后存储结果(查找单词just-in-time)。这样做的好处是你可以得到一本普通话词典,然后在中国做同样的实验,而不必保留十个翻译短语的便签(不同平台的二进制文件)。

The third is where you look up the words while you are standing in front of each person. In essence, you interpret the words for each person separately (you act as an interpreter). The advantage here is that any changes are instantly reflected with the next person (you could change to asking "Hello. What color is your dog?" without having to fly home and restart - you don't need to recompilethe phrases).

第三个是当你站在每个人面前时,你可以查看单词。本质上,您分别为每个人解释单词(您充当翻译)。这里的优点是任何更改都会立即反映给下一个人(您可以改为询问“您好。您的狗是什么颜色?”而不必飞回家并重新启动 - 您不需要重新编译短语)。

  • Translating beforehand means you can ask people fastest (pre-compiliation); you don't need to even bring the dictionary with you.
  • Translating when you see the first person in each country is almost as fast as translating beforehand but still allows you to travel to multiple countries without needing to go home to get a dictionary but means that you need to bring several dictionaries with you (a platform independent runtime).
  • Translating on demand is much slower but allows you to change the words without traveling home (source distributed language).
  • 预先翻译意味着你可以最快地问人(编译);你甚至不需要随身携带字典。
  • 当你看到每个国家的第一个人时翻译几乎和事先翻译一样快,但仍然允许你去多个国家旅行而无需回家拿字典,但这意味着你需要随身携带几本字典(独立于平台的)运行时)。
  • 按需翻译要慢得多,但允许您在不回家的情况下更改单词(源分布式语言)。

回答by del bao

tl;dr

tl;博士

Interpreter: takes only one instruction at a time for execution

解释器:一次只执行一条指令

Just-in-time: takes a block of code at once and compile it before execute. so has plenty room for optimization

Just-in-time:立即获取一个代码块并在执行前编译它。所以有很大的优化空间