Operating systems / Compilers vs interpreters

Compilers vs interpreters

Two ways to get from text a person wrote to instructions a processor can run: translate everything first, or work through it while running. Plus the mix most languages use today.

Beginner8 steps~7 min
Source code — the program as people write it
Compiler — translates the whole program up front
Interpreter — carries the program out step by step
CPU — runs machine code and nothing else
// THE SHORT VERSION

In 30 seconds

A processor only runs machine code, so source code has to be translated. A compiler translates the whole program ahead of time into a file the processor runs directly. An interpreter reads the program and carries it out step by step, each time it runs.

Compiled programs usually run faster; interpreted ones are quicker to change and move between machines.

Most modern languages mix the two: they compile to a compact bytecode, and a JIT compiler turns the busiest parts into machine code while the program runs.

Compilers vs interpreters, step by step

STEPS01 / 08

The example is pseudocode. The x86 and Java bytes shown are real encodings, assuming sum and i sit in the registers eax and ecx, or in local slots 1 and 2. Some implementations of Python, Ruby and Lua also have a JIT.

STEP 01 / 08 The CPU cannot read your code
THE MATH
sum = sum + i = add eax, ecx = 01 C8 (x86 bytes)
  1. Step 1: The CPU cannot read your code. Programs are written as source code, text meant for people. A processor only carries out machine code: short numeric instructions specific to its design. On an x86 chip, adding one number to another can be the two bytes 01 C8. Something has to translate, and the question is when.
  2. Step 2: Compiler: translate first. A compiler reads the entire program and writes out an executable: a file of machine code. This happens once, ahead of time. Afterwards the executable runs directly on the processor as often as you like, and neither the compiler nor the source code is needed to run it.
  3. Step 3: Interpreter: run it directly. An interpreter is a program that reads your source code and carries it out itself, one statement after another. No machine-code file of your program is produced. It starts at once, but the interpreter has to be installed wherever the program runs, and it does its reading while the program is running.
  4. Step 4: Speed: translate once or often. The loop body runs three times. Compiled, it was translated once, before the start, and each pass is pure work. Interpreted, every pass begins by working out again what the line means. In long loops that repeated effort dominates, and interpreted code is often many times slower.
  5. Step 5: Flexibility: what you get back. An executable is tied to one kind of processor and operating system: built for x86, it will not run on an ARM chip until it is compiled again. Source code handed to an interpreter runs unchanged wherever that interpreter exists, and after an edit you simply run it again, with no build step.
  6. Step 6: Middle ground: bytecode. Many languages do both. A compiler first turns the source into bytecode: simple, compact instructions for an imaginary processor. A program called a virtual machine then interprets the bytecode. Decoding it is far quicker than re-reading text, and one bytecode file runs on any computer that has the VM.
  7. Step 7: JIT: compile while running. A just-in-time (JIT) compiler sits inside the virtual machine. The VM starts by interpreting and counts how often each piece of code runs. Code that turns out to be busy, such as our loop body in a long loop, is compiled to real machine code on the spot, so later passes run at close to compiled speed.
  8. Step 8: Where languages usually sit. Few languages are purely one or the other. C, C++, Rust and Go are normally compiled ahead of time. Java, C# and JavaScript engines use bytecode with a JIT. The standard Python and Ruby interpreters compile to bytecode and interpret that. It is the implementation that decides, and one language can have several.
VALUES SO FAR
linesum = sum + isource
x8601 C8machine
compiler····when
output····result
interp.····when
loop····example
bytecode····Java
JIT····compiles

Spotted a mistake on this page?