Interpreter vs Compiler vs Just In Time (JIT) Compiler
Blog / Interpreter vs Compiler vs Just In Time (JIT) Compiler
So what exactly is the difference between an interpreter, a compiler, and a just in time compiler?TL;DR Summary Interpreter An interpreter is a program, that translates the source code into intermediate code and executes that code line by line at runtime. This allows for immediate execution but is typically slower running due to the translation overhead during execution.Compiler A compiler on the other hand, translates the entire source code into machine code before execution, this results in faster runtime performance because the code is already in executable form. However, this has the downside of of longer initial development cycle due to the time-consuming compilation step.It’s important to remember a traditional compiler compiles all the code to machine code before the program is first run.JIT Compiler Just-In-Time (JIT) compilation bridges these approaches of interpreting and compiling by compiling code after a program has begun running (during execution).There are many different implementations of a JIT, however typically it It starts by interpreting the code and then the JIT compiler will compiles frequently executed parts into machine code on the fly, combining the immediacy of interpretation with the performance benefits of compilation.Unlike a standard compiler, a JIT compiler leverages dynamic runtime information to optimize the code more effectively. For example it can inline frequently used functions which in turn can improve performance.So lets look at a real world example of a just in time compiler:
- An interpreter, translates the source code into intermediate code or directly into machine instructions and executes that code line by line at runtime.
- A compiler on the other hand, translates the entire source code into machine code before execution.
- Just-In-Time (JIT) compiler compiles code after a program has begun running (during execution).
Don't let one question ruin your next technical interview...
- Lets say you’ve written come Java source code in
.java
files. - Use the
javac
compiler to compile the source code into platform-independent bytecode, generating.class
files. - The JVM’s class loader loads the
.class
files into memory, dynamically loading classes at runtime. - The JVM identifies frequently executed code paths (hot spots).
- The JIT compiler translates these hot spots from bytecode to optimized native machine code.
- Subsequent executions of hot spots use the optimized machine code, executed directly by the CPU, significantly improving performance.