与非JIT编译器相比,JIT编译器具体做什么?有人能给出简洁易懂的描述吗?
当前回答
20%的字节码在80%的时间内被使用。JIT编译器获得这些统计数据,并优化这20%的字节代码,通过添加内联方法、删除未使用的锁等,并创建特定于这台机器的字节码来更快地运行。我引用这篇文章,我发现它很方便。http://java.dzone.com/articles/just-time-compiler-jit-hotspot
其他回答
JVM actually performs compilation steps during runtime for performance reasons. This means that Java doesn't have a clean compile-execution separation. It first does a so called static compilation from Java source code to bytecode. Then this bytecode is passed to the JVM for execution. But executing bytecode is slow so the JVM measures how often the bytecode is run and when it detects a "hotspot" of code that's run very frequently it performs dynamic compilation from bytecode to machinecode of the "hotspot" code (hotspot profiler). So effectively today Java programs are run by machinecode execution.
正如其他人提到的
JIT代表Just-in-Time,这意味着代码在需要时才编译,而不是在运行时之前。
为了给上面的讨论补充一点,JVM维护一个函数执行时间的计数。如果该计数超过预定义的限制,JIT将代码编译为处理器可以直接执行的机器语言(不像一般情况下,javac将代码编译为字节码,然后java -解释器逐行解释这个字节码,将其转换为机器代码并执行)。
另外,下次计算此函数时,将再次执行相同的编译代码,而不像常规解释那样逐行重新解释代码。这使得执行速度更快。
jit——来得正是时候 这个词本身就表示需要的时候(按需)
典型场景:
源代码完全转换为机器代码
JIT的场景:
源代码将转换为汇编语言,如结构[为ex IL(中间语言)为c#,字节码为java]。
中间代码仅在应用程序需要时才转换为机器语言,所需代码仅转换为机器代码。
JIT与非JIT的比较:
在JIT中,并不是所有的代码都先转换成机器码 所有必要的代码都将被转换成机器代码 如果调用的方法或功能不在机器中,那么 将被转换成机器代码…减少CPU的负担。 因为机器代码将在运行时....上生成JIT 编译器将生成优化运行的机器代码 机器的CPU架构。
JIT的例子:
在Java中,JIT是在JVM (Java虚拟机)中 在c#中,它是在CLR(公共语言运行库)中 在Android中,它是在DVM (Dalvik Virtual Machine)中,或者在更新的版本中是ART (Android RunTime)。
即时编译器(JIT)是一种软件,它接收一个不可执行的输入,并返回要执行的适当机器代码。例如:
Intermediate representation JIT Native machine code for the current CPU architecture
Java bytecode ---> machine code
Javascript (run with V8) ---> machine code
其结果是,对于特定的CPU体系结构,必须安装适当的JIT编译器。
区别编译器、解释器和JIT
虽然在一般情况下,当我们想要将源代码转换为机器码时可能会有例外,但我们可以使用:
Compiler: Takes source code and returns a executable Interpreter: Executes the program instruction by instruction. It takes an executable segment of the source code and turns that segment into machine instructions. This process is repeated until all source code is transformed into machine instructions and executed. JIT: Many different implementations of a JIT are possible, however a JIT is usually a combination of a compiler and an interpreter. The JIT first turn intermediary data (e.g. Java bytecode) which it receives into machine language via interpretation. A JIT can often measures when a certain part of the code is executed often and the will compile this part for faster execution.
代码被编译成某种IL(中间语言)。当你运行你的程序时,计算机不理解这段代码。它只理解本机代码。因此JIT编译器会动态地将IL编译为本地代码。它在方法级别上执行此操作。
推荐文章
- 警告:API ' variable . getjavacompile()'已过时,已被' variable . getjavacompileprovider()'取代
- 阅读GHC核心
- 解析.py文件,读取AST,修改它,然后写回修改后的源代码
- 是否可以动态编译和执行c#代码片段?
- 为什么Java类的编译与空行不同?
- 如何使用cmake创建共享库?
- 在C和c++中静态变量存储在哪里?
- 无法加载文件或程序集…参数不正确
- 如果使用if-return-return或if-else-return?
- Java是编译型编程语言还是解释型编程语言?
- 为什么Swift的编译时间这么慢?
- 选项(RECOMPILE)总是更快;为什么?
- 如何在Angular 2.0中使用/创建动态模板来编译动态组件?
- 如何修复从源代码安装PHP时未发现xml2-config的错误?
- 用自己的语言编写编译器