6种编译方式:
字多不看版:
默认。解释型。解释器在要真的要干活时才开始编译代码。
Just-In-Time (JIT) compilation refers to a form of dynamic program translation that compiles code during program execution, rather than prior to execution, which is how traditional compilers work. This approach can optimize the program to the actual workload and system environment, potentially leading to higher performance. Here’s how JIT compares to other compilation and execution strategies:
-
Ahead-of-Time (AOT) Compilation:先全部编译成机器码。由于不着急得到运算结果,可以花时间进行代码分析和大量优化。编译出来又快又好。 This is the traditional method where the source code is compiled into machine code before the program is run. Examples include C and C++ compilers like GCC and Clang. AOT compilers have the luxury of spending more time analyzing and optimizing the code because they do not have to deliver immediate results during runtime.
-
Interpretation: An interpreter directly executes the instructions written in a programming or scripting language without previously converting it to machine code. Python, in its default CPython implementation, is an interpreted language, though it actually interprets bytecode that has been compiled from source code.
-
Bytecode Interpretation: 字节码解释。中间状态:字节码,由虚拟机执行。(Java狂喜) This is a middle ground between AOT and JIT. The source code is compiled into an intermediate form called bytecode, which is then executed by a virtual machine. Java and the default Python interpreter (CPython) work like this. The bytecode is generally portable across different platforms, and it’s faster to interpret than high-level source code.
-
JIT Compilation: 执行时再编译成机器码 JIT compilers, like those found in PyPy (an alternative Python implementation), the HotSpot Java Virtual Machine, and .NET’s CLR, compile the bytecode or intermediate representation (IR) to machine code on the fly, during execution. This allows them to optimize the code based on current usage patterns and the current environment.
-
Transpilation: 语言转换(编译): TS变身JS. This involves converting source code from one language to another. It’s not strictly a form of compilation to machine code, but it’s worth mentioning. An example is TypeScript being transpiled to JavaScript.
-
Hybrid Approaches: 复合策略。现代JVM一起用JIT和AOT预先编译。Some systems use a combination of these strategies. For example, modern Java Virtual Machines use both JIT and AOT compilation to optimize performance.
In Python, the typical execution model involves compiling Python source code (.py files) into bytecode (.pyc files), which is then interpreted by the Python virtual machine (CPython). This is primarily an interpreted model with a bytecode compilation step. However, there are alternative implementations of Python that use JIT compilation:
-
PyPy: This is the most well-known Python JIT compiler. It aims to be a drop-in replacement for CPython. PyPy can significantly speed up the execution of Python programs, sometimes by several times, because it compiles parts of the program at runtime to machine code.
-
Numba: This is a JIT compiler that translates a subset of Python and NumPy code into fast machine code. It’s primarily used for numerical functions and can be used with the standard CPython interpreter.
-
IronPython and Jython: These are Python implementations for the .NET framework and the Java Virtual Machine (JVM), respectively. They can take advantage of the JIT compilation provided by the CLR and JVM.
JIT compilation can provide a significant performance boost for certain types of applications, especially those with hot loops or frequently called functions where the overhead of dynamic typing and interpretation can be minimized through runtime analysis and optimization.
数据对比:
以后再做吧~