即时编译和提前编译
Just-in-time compilation is a method for improving the performance of interpreted programs. During execution the program may be compiled into native code to improve its performance. It is also known as dynamic compilation.
即时编译是一种提高解释程序性能的方法。 在执行期间,可以将程序编译为本机代码以提高其性能。 也称为动态编译。
Dynamic compilation has some advantages over static compilation. When running Java or C# applications, the runtime environment can profile the application while it is being run. This allows for more optimized code to be generated. If the behavior of the application changes while it is running, the runtime environment can recompile the code.
动态编译比静态编译具有一些优势。 在运行Java或C#应用程序时,运行时环境可以在运行应用程序时对其进行概要分析。 这允许生成更多优化的代码。 如果应用程序在运行时的行为发生了变化,则运行时环境可以重新编译代码。
Some of the disadvantages include startup delays and the overhead of compilation during runtime. To limit the overhead, many JIT compilers only compile the code paths that are frequently used.
一些缺点包括启动延迟和运行时编译的开销。 为了限制开销,许多JIT编译器仅编译经常使用的代码路径。
总览 (Overview)
Traditionally there are two methods for converting source code into a form that can be run on a platform. Static compilation converts the code into a language for a specific platform. An interpreter directly executes the source code.
传统上,有两种方法可以将源代码转换为可以在平台上运行的形式。 静态编译将代码转换为特定平台的语言。 解释器直接执行源代码。
JIT compilation attempts to use the benefits of both. While the interpreted program is being run, the JIT compiler determines the most frequently used code and compiles it to machine code. Depending on the compiler, this can be done on a method or smaller section of code.
JIT编译尝试利用两者的好处。 在运行解释程序时,JIT编译器确定最常用的代码并将其编译为机器代码。 根据编译器的不同,可以在方法或代码的较小部分上完成此操作。
Dynamic compilation was first described in a paper by J. McCarthy on LISP in 1960.
动态编译最初是由J. McCarthy在1960年关于LISP的论文中描述的。
Just In Time Compilation, JIT, or Dynamic Translation, is compilation that is being done during the execution of a program. Meaning, at run time, as opposed to prior to execution. What happens is the translation to machine code. The advantages of a JIT are due to the fact, that since the compilation takes place in run time, a JIT compiler has access to dynamic runtime information enabling it to make better optimizations (such as inlining functions).
即时编译,JIT或动态翻译是在程序执行期间进行的编译。 意思是在运行时,而不是在执行之前。 发生的是翻译成机器代码。 JIT的优点是由于以下事实:既然编译是在运行时进行的,那么JIT编译器可以访问动态运行时信息,从而可以进行更好的优化(例如内联函数)。
What is important to understand about the JIT compilation, is that it will compile the bytecode into machine code instructions of the running machine. Meaning, that the resulting machine code is optimized for the running machine’s CPU architecture.
要了解有关JIT编译的重要信息,那就是它将字节码编译为正在运行的机器的机器代码指令。 这意味着,生成的机器代码已针对正在运行的机器的CPU架构进行了优化。
Some examples of JIT Compilers are JVM (Java Virtual Machine) in Java and CLR (Common Language Runtime), in C#.
JIT编译器的一些示例是Java中的JVM(Java虚拟机)和C#中的CLR(公共语言运行时)。
历史 (History)
In the beginning, a compiler was responsible for turning a high-level language (defined as higher level than assembler) into object code (machine instructions), which would then be linked (by a linker) into an executable.
最初,编译器负责将高级语言(定义为比汇编器高的语言)转换为目标代码(机器指令),然后将其(通过链接器)链接为可执行文件。
At one point in the evolution of languages, compilers would compile a high-level language into pseudo-code, which would then be interpreted (by an interpreter) to run your program. This eliminated the object code and executables, and allowed these languages to be portable to multiple operating systems and hardware platforms. Pascal (which compiled to P-Code) was one of the first; Java and C# are more recent examples. Eventually the term P-Code was replaced with bytecode, since most of the pseudo-operations are a byte long.
在语言发展的某一时刻,编译器会将高级语言编译为伪代码,然后将其解释(由解释器)以运行您的程序。 这消除了目标代码和可执行文件,并允许这些语言可移植到多个操作系统和硬件平台。 Pascal(编译为P-Code)是最早的一个。 Java和C#是最近的示例。 最终,术语P-Code替换为字节码,因为大多数伪操作都是一个字节长。
A Just-In-Time (JIT) compiler is a feature of the run-time interpreter, that instead of interpreting bytecode every time a method is invoked, will compile the bytecode into the machine code instructions of the running machine, and then invoke this object code instead. Ideally the efficiency of running object code will overcome the inefficiency of recompiling the program every time it runs.
即时(JIT)编译器是运行时解释器的功能,它不是在每次调用方法时都解释字节码,而是将字节码编译为正在运行的计算机的机器代码指令,然后调用此代码。目标代码。 理想情况下,运行目标代码的效率将克服每次运行时重新编译程序的效率低下的问题。
典型场景 (Typical scenario)
The source code is completely converted into machine code
源代码已完全转换为机器代码
JIT场景 (JIT scenario)
The source code will be converted into assembly language like structure [for ex IL (intermediate language) for C#, ByteCode for java].
源代码将被转换为汇编语言,如结构[对于C#来说,是ex IL(中间语言),对于Java是ByteCode)。
The intermediate code is converted into machine language only when the application needs that is required codes are only converted to machine code.
仅当应用程序需要的中间代码仅转换为机器代码时,才将中间代码转换为机器语言。
JIT与非JIT比较 (JIT vs Non-JIT comparison)
In JIT not all the code is converted into machine code first a part of the code that is necessary will be converted into machine code then if a method or functionality called is not in machine then that will be turned into machine code, which reduces burden on the CPU. As the machine code will be generated on run time, the JIT compiler will produce machine code that is optimized for running machine’s CPU architecture.
在JIT中,并非所有代码都首先转换为机器代码,一部分必需的代码将被转换为机器代码,然后,如果所调用的方法或功能不在机器中,则将其转换为机器代码,从而减轻了负担CPU。 由于机器代码将在运行时生成,因此JIT编译器将生成针对机器的CPU体系结构进行了优化的机器代码。
Some examples of JIT are:
JIT的一些示例是:
- Java: JVM (Java Virtual Machine) Java:JVM(Java虚拟机)
- C#: CLR (Common Language Runtime) C#:CLR(公共语言运行时)
- Android: DVM (Dalvik Virtual Machine) or ART (Android RunTime) in newer versions Android:较新版本的DVM(Dalvik虚拟机)或ART(Android运行时)
The Java Virtual Machine (JVM) executes bytecode and maintains a count as of how many time a function is executed. If this count exceeds a predefined limit JIT compiles the code into machine language which can directly be executed by the processor (unlike the normal case in which javac compiles the code into bytecode and then Java, the interpreter interprets this bytecode line by line converts it into machine code and executes).
Java虚拟机(JVM)执行字节码并维护一个函数执行多少次的计数。 如果此计数超过预定义的限制,则JIT会将代码编译为机器语言,该语言可以直接由处理器执行(不同于正常情况下,javac将该代码编译为字节码,然后由Java编译,解释器逐行解释此字节码,将其转换为机器代码并执行)。
Also next time this function is calculated same compiled code is executed again unlike normal interpretation in which the code is interpreted again line by line. This makes execution faster.
同样,下次计算此函数时,将再次执行相同的编译代码,这与正常解释不同,在正常解释中,代码逐行再次解释。 这使执行速度更快。
翻译自: https://www.freecodecamp.org/news/just-in-time-compilation-explained/
即时编译和提前编译