Computer Science • Programming languages • 7 min read

Compiler vs Interpreter: What's the Difference?

Summary

A compiler translates a whole program into another form, usually machine code, before the program runs. An interpreter reads a program and carries out its instructions directly while it runs. In practice most modern languages use both: they compile to an intermediate form and then interpret or just-in-time compile it.

What compilers and interpreters do

Source code is text written for humans. A CPU cannot run it. Something has to turn it into actions the machine can perform. Compilers and interpreters are the two basic ways to do that.

The core difference is when the translation happens and what it produces:

  • A compiler translates the program ahead of time and produces a new artifact: an executable file, a library, or bytecode.
  • An interpreter reads the program and performs its operations directly during the run. It does not produce a standalone executable.

How a compiler works

A compiler is a pipeline of stages. Exact designs vary, but most follow this shape:

  1. Lexing. The source text is split into tokens such as keywords, identifiers, numbers, and operators.
  2. Parsing. Tokens are arranged into a tree (an abstract syntax tree, or AST) that reflects the grammar of the language.
  3. Semantic analysis. The compiler checks meaning: are variables declared, do the types match, are functions called with the right arguments?
  4. Intermediate representation (IR). The AST is converted into a simpler, lower-level form that is easier to analyze and transform.
  5. Optimization. The compiler rewrites the IR to run faster or use less memory, for example by removing unused code or computing constant expressions in advance.
  6. Code generation. The IR is translated into instructions for a target, usually a specific CPU architecture.
  7. Linking. Separate pieces of compiled code and libraries are joined into one program.

The output can be run many times without translating again. Typical examples are C and C++ (with GCC or Clang), Rust (rustc), and Go (go build).

How an interpreter works

An interpreter also needs to understand the program, so it usually lexes and parses it too. The difference comes after that. Instead of generating a file of machine instructions, it executes the program itself.

There are two common designs:

  • Tree-walking interpreter. It walks the AST and evaluates each node directly. This is simple to build but relatively slow.
  • Bytecode virtual machine. The source is first converted into compact bytecode, and a loop inside the interpreter (the virtual machine) executes that bytecode one instruction at a time. This is faster and is the more common design.

Because the interpreter is itself a program that must be running, every execution needs the interpreter present on the machine.

A small example

x = 2 + 3
print(x)

A compiler would translate these two lines once, ahead of time, into instructions for the target machine. Those instructions can run any number of times without translating again. An interpreter would read the lines each time you run the program, evaluate 2 + 3, store the result, and call the print routine as it goes. A good compiler might also notice that 2 + 3 is always 5 and compute it at compile time.

Side-by-side comparison

AspectCompilerInterpreter
When translation happensBefore the program runsDuring the run
Typical outputExecutable, object file, or bytecodeNo standalone output
Startup timeFast once built; build step takes timeStarts immediately, no build step
Raw execution speedUsually fasterUsually slower
Optimization scopeCan analyze the whole programLimited, unless combined with a JIT
DistributionShip a binary (specific to a platform)Ship source or bytecode plus an interpreter
Edit-and-run cycleCompile, then runRun directly; often an interactive prompt (REPL)

Language vs implementation

“Compiled language” and “interpreted language” are shortcuts, and they can mislead. Compiling or interpreting is a property of an implementation, not of the language itself.

  • C is normally compiled, but C interpreters exist.
  • Python is usually run with CPython, which compiles source to bytecode and then interprets it. PyPy is another Python implementation that uses a just-in-time compiler.
  • JavaScript was historically interpreted, but modern engines such as V8 compile hot code to machine code while the program runs.

A language can be designed in a way that makes one approach easier. Languages with static types and fixed layouts are easier to compile efficiently. Languages that can change types and structures at runtime are harder to compile well. Still, the choice belongs to the implementation.

Hybrid approaches

Most widely used systems sit between the two extremes.

  • Bytecode compilation plus a virtual machine. Java source is compiled by javac into bytecode. The Java Virtual Machine (JVM) then runs that bytecode. CPython works similarly: it compiles to bytecode first (you may have seen .pyc files) and then runs it in a VM.
  • Just-in-time (JIT) compilation. The runtime starts by interpreting, watches which code runs often, and compiles those “hot” parts to machine code while the program is running. The JVM and JavaScript engines do this. It can approach the speed of ahead-of-time compilation, at the cost of warm-up time and extra memory.
  • Ahead-of-time (AOT) compilation. Languages that normally use a VM can sometimes be compiled to native code before running. This is the traditional compiler model applied to a more dynamic language.
  • Transpilers. A source-to-source compiler translates one language into another at a similar level, such as TypeScript into JavaScript.

Trade-offs in practice

Speed. A compiler can spend as much time as it needs optimizing, once, before the program ships. An interpreter pays translation and dispatch costs while running. This is why compiled code is usually faster, though a JIT can narrow the gap for long-running programs.

Error detection. Compilers can report many problems before the program ever runs, such as syntax errors and type mismatches. Interpreters typically report an error only when execution reaches the faulty line. Note that this comes from the language’s checks, not from compiling alone: static analyzers and type checkers for interpreted languages (for example, mypy for Python) can catch similar problems early.

Portability. A compiled binary targets a specific CPU architecture and operating system. The same source must be compiled again for each target. Interpreted programs, and bytecode run on a VM, work on any platform that has the interpreter or VM.

Development speed. Interpreters make quick experiments easy: change a line, run it, or try it in a REPL. Compiled workflows add a build step, though incremental builds keep this short in many projects.

Debugging. An interpreter has direct access to the running program’s structure, so inspecting and modifying state is often straightforward. Debugging compiled code relies on debug information that the compiler must be asked to include, and heavy optimization can make the running code differ from the source you wrote.

Common misconceptions

  • “Interpreted languages are always slow.” Speed depends on the implementation and the workload. JIT-compiled runtimes can be fast, and a lot of real programs spend most of their time in libraries written in compiled languages.
  • “A compiler always produces machine code.” Many compilers target bytecode or another high-level language instead.
  • “An interpreter does no translation.” Most interpreters parse the source and often convert it to bytecode first. They just don’t produce a separate program for you to run.
  • “Python has no compile step.” CPython compiles source to bytecode automatically before running it. You usually don’t see this step.
  • “Compiled means no runtime needed.” Compiled programs can still depend on a runtime or libraries, and some languages ship a garbage collector or scheduler inside the binary.

Key takeaways

  • A compiler translates a program ahead of time into another form; an interpreter executes the program directly during the run.
  • Compiled code usually runs faster and can be checked and optimized as a whole; interpreted code starts quickly and is easier to experiment with.
  • Whether a language is “compiled” or “interpreted” depends on its implementation, and one language can have several.
  • Modern systems often combine both through bytecode, virtual machines, and JIT compilation.
  • Choose by constraints, such as performance needs, target platforms, and iteration speed, rather than by the label.

Related articles in Computer Science

Image preview

Fit