Compilation, interpretation and performance
A processor does not directly execute JavaScript, Python or C. A tool must transform source code into usable instructions.
Compilation before execution
A compiler analyzes a program and produces another artifact before it execution: machine code, bytecode or intermediate representation.
#include <stdio.h>
int main(void) {
printf("Bonjour !\n");
return 0;
}
gcc programme.c -o programme
./programme
The compiler can detect many errors and optimize the entire program before its launch.
Interpretation
An interpreter controls execution from the program and its environment of execution. The expression “line by line” is useful for getting started, but it is incomplete: the tool generally analyzes the code and can produce a form intermediate.
print("Bonjour !")
Python notably compiles the code into bytecode before the virtual machine executes it.
Modern engines combine approaches
Modern JavaScript engines analyze code, produce intermediate representations and often compile portions on the fly executed. We are talking about JIT (just in time) compilation.
It is therefore more accurate to ask three questions:
- what transformation is done before execution?
- which environment executes the result?
- What optimizations are performed during execution?
Analogy
- compiling is like preparing and re-reading an entire itinerary before leaving;
- interpreting is like traveling with a guide who reads and carries out the steps;
- a JIT engine observes repeated paths and prepares shortcuts for them the most frequent passages.