Compiled vs interpreted: what the words mean
Almost every popular language compiles your code. The difference that matters is when the optimizer runs: before the program starts, while it runs, or never.
Four things people call compiled or interpreted
- A compiler translates your source into another form, either machine code or bytecode.
- Machine code runs directly on the CPU. C++, Rust, Go and Swift compilers produce it before the program starts.
- Bytecode runs on a virtual machine. A JIT compiler inside the virtual machine can turn hot bytecode into machine code while the program runs. Java, C#, JavaScript on V8 and PyPy do this.
- A real interpreter executes the source text or the syntax tree directly and repeats the translation work as it goes. Shell scripts, early BASIC, Ruby 1.8 and Tcl before version 8.0 worked this way.
| Kind | Examples | When the optimizer works |
|---|---|---|
| Machine code | C++, Rust, Go, Swift | Before the program runs |
| Bytecode with a JIT | Java, C#, JavaScript on V8, PyPy | While the program runs |
| Bytecode with no JIT | CPython, standard Lua, Tcl | Never makes machine code |
| Real interpreter | Shell scripts | Does not apply |
Why “interpreted” misleads
Python's documentation calls its virtual machine the interpreter, so many people call Python interpreted even though it compiles to bytecode first. The label also hides what decides speed. In our test, a Java program took 3,235 ns with the JIT off and 40 ns with it on. C++ took 2,027 ns with no optimization and 106 ns at -O2. See the measured times.
Questions
Is Python compiled or interpreted?
Python is compiled to bytecode, and a virtual machine runs the bytecode. CPython has no JIT by default.
Does compiled mean fast?
No. Compiled code is fast only when an optimizer works on it. C++ built with no optimization took 2,027 ns on our program, slower than CPython at 1,531 ns.
What is a JIT compiler?
A just-in-time compiler turns frequently run code into machine code while the program runs. HotSpot, V8 and PyPy have one.
What is bytecode?
Bytecode is a compact instruction format that a virtual machine runs. Compilers for Java, Python, Ruby and PHP produce it.