Back to series
· 8 min read

Compiled vs. Interpreted Languages


1. What Are Compiled Languages and Interpreted Languages?

The source code of a programming language must ultimately be transformed into operations a computer can execute through some execution mechanism.

Depending on how code is converted and executed, programming languages are traditionally divided into two categories:

  • Compiled Language: usually converts source code into target code first, then executes the resulting program.
  • Interpreted Language: usually an interpreter reads and executes the source code or an intermediate representation while the program is running.

Note: this is a traditional classification, not a strict property of a language. Modern language runtimes commonly employ multiple mechanisms such as compilation, interpretation, and just-in-time compilation; a language does not necessarily use only one execution approach.

For example:

Language Common execution approach
C Compiled to machine code, then executed
C++ Compiled to machine code, then executed
Rust Compiled to machine code, then executed
Go Compiled to machine code, then executed
Python Usually compiled to bytecode first, then executed by a virtual machine
JavaScript Usually interpreted and executed by an engine, in combination with JIT compilation
Java Compiled to bytecode, executed by the JVM, optionally with JIT compilation
C# Compiled to IL, executed by the CLR, optionally with JIT compilation

2. How Compiled Languages Execute

1. What Is Compilation?

Compilation is the process of converting source code written in one programming language into another form of code.

In common compiled languages, the compiler converts high-level source code into machine code or object files for the target machine.

Take the C language as an example:

#include <stdio.h>

int main() {
    printf("Hello World\n");
    return 0;
}

This code cannot be executed directly by the CPU; it must go through steps such as compilation and linking to finally produce an executable file.

2. The Execution Flow of Compiled Languages

A typical C build process is as follows:

flowchart TB
    A["C source code"] --> B["Preprocessing"]
    B --> C["Compilation"]
    C --> D["Assembly"]
    D --> E["Object file"]
    E --> F["Linking"]
    F --> G["Executable file"]
    G --> H["OS loading"]
    H --> I["CPU execution"]

    style A fill:#E5F4EC,stroke:#A3CBB5,color:#334155
    style B fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style C fill:#F3EDFC,stroke:#C4B2E3,color:#334155
    style D fill:#F3EDFC,stroke:#C4B2E3,color:#334155
    style E fill:#FFF5DE,stroke:#E8D29A,color:#334155
    style F fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style G fill:#E5F4EC,stroke:#A3CBB5,color:#334155
    style H fill:#E8EDF2,stroke:#B8C3CE,color:#334155
    style I fill:#E5F4EC,stroke:#A3CBB5,color:#334155
    linkStyle default stroke:#A3B8AC,stroke-width:1.5px

The main stages are as follows:

(1) Preprocessing

Processes the preprocessing directives in the source code, for example:

  • Expanding #include.
  • Expanding #define macros.
  • Handling conditional compilation directives.
  • Removing comments, and so on.

(2) Compilation

Converts the preprocessed source code into assembly code.

A compiler typically performs:

  • Lexical analysis.
  • Syntax analysis.
  • Semantic analysis.
  • Intermediate representation generation.
  • Optimization.
  • Target code generation.

(3) Assembly

Converts assembly code into machine instructions, producing an object file.

An object file typically contains:

  • Machine code.
  • Symbol table.
  • Relocation information.
  • Debug information.
  • Data sections, and so on.

(4) Linking

Combines multiple object files and the libraries they depend on, resolves external symbol references, and produces the final executable file or dynamic library.

Linking is usually divided into:

  • Static linking: the required library code is copied or merged into the final artifact.
  • Dynamic linking: the required shared libraries are loaded by the system when the program runs.

(5) Execution

The operating system loads the executable file and sets up the process runtime environment; the CPU then executes the machine instructions within it.

3. Main Characteristics of Compiled Languages

  • Relatively high runtime efficiency: the compiler can optimize before runtime and generate machine code suitable for the target platform.
  • Relatively independent execution: once the program is compiled, it can usually run without carrying the full compiler.
  • Deployment needs to consider the platform: machine code is usually related to the operating system, CPU architecture, ABI, and other environment factors.
  • Some errors can be found during compilation: syntax errors, type errors, and some static analysis issues can be exposed during the build stage.
  • The build process can be complex: large projects need to handle compilation, linking, dependency management, cross-compilation, and other issues.

Note that compilation does not mean the program requires no other environment at runtime. Dynamic libraries, runtime libraries, operating system interfaces, etc. may still be necessary for the program to run.

3. Execution Principles of Interpreted Languages

1. What Is Interpreted Execution?

Interpreted execution (Interpretation) means that, when a program runs, an interpreter reads the source code or intermediate representation and performs the corresponding operations according to the language semantics.

A traditional interpreter performs instruction parsing, operation execution, and runtime state management during execution.

For example, Python:

def add(a, b):
    return a + b

print(add(10, 20))

Developers can run Python files directly:

python main.py

There is no need, as with C, for the developer to first explicitly generate a separate native executable file.

2. Execution Flow of Interpreted Languages

The traditional interpreted execution process can be abstracted as:

flowchart TB
    A["Source code"] --> B["Lexical analysis"]
    B --> C["Syntax analysis"]
    C --> D["Generate syntax structure or intermediate representation"]
    D --> E["Interpreter execution"]
    E --> F["Runtime environment"]
    F --> G["Program output"]

    style A fill:#E5F4EC,stroke:#A3CBB5,color:#334155
    style B fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style C fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style D fill:#F3EDFC,stroke:#C4B2E3,color:#334155
    style E fill:#FFF5DE,stroke:#E8D29A,color:#334155
    style F fill:#E8EDF2,stroke:#B8C3CE,color:#334155
    style G fill:#E5F4EC,stroke:#A3CBB5,color:#334155
    linkStyle default stroke:#A3B8AC,stroke-width:1.5px

Actual interpreters do not necessarily execute strictly step by step according to the above process. Implementations for different languages may first generate an abstract syntax tree, bytecode, or another intermediate representation, and then have a virtual machine execute it.

3. Python's Execution Mechanism

Python is a typical example that is easily misunderstood.

Many people think that Python is a purely interpreted language and that its source code is executed line by line directly by the interpreter.

In fact, the mainstream Python implementation CPython usually includes two stages: compilation and interpretation.

flowchart TB
    A["Python source code .py"] --> B["Lexical and syntax analysis"]
    B --> C["Generate AST"]
    C --> D["Compile to bytecode"]
    D --> E["Python virtual machine"]
    E --> F["Execute bytecode"]
    F --> G["Execution result"]

    style A fill:#E5F4EC,stroke:#A3CBB5,color:#334155
    style B fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style C fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style D fill:#F3EDFC,stroke:#C4B2E3,color:#334155
    style E fill:#FFF5DE,stroke:#E8D29A,color:#334155
    style F fill:#E8EDF2,stroke:#B8C3CE,color:#334155
    style G fill:#E5F4EC,stroke:#A3CBB5,color:#334155
    linkStyle default stroke:#A3B8AC,stroke-width:1.5px

Here:

  • AST (Abstract Syntax Tree): an abstract syntax tree that represents the syntactic structure of a program.
  • Bytecode: a form of intermediate instructions designed for a virtual machine.
  • Python virtual machine: the runtime component responsible for executing bytecode.

CPython may cache compiled bytecode as .pyc files to reduce repeated compilation work during subsequent loads.

However, .pyc files are not machine code that the CPU can execute directly; they still need the Python virtual machine to execute them.

4. Main Characteristics of Interpreted Languages

  • Faster development feedback: source code can usually be run directly, reducing explicit build steps.
  • Stronger cross-platform capability: as long as the target platform provides a compatible interpreter or runtime, the program can potentially run.
  • Potentially higher runtime overhead: operations such as dynamic type checking, interpreter dispatch, and dynamic dispatch may increase execution costs.
  • Greater runtime flexibility: some languages support dynamic loading, reflection, dynamic typing, and runtime modification of behavior.
  • Obvious dependence on the runtime environment: the corresponding interpreter, virtual machine, and dependency libraries usually need to be installed or provided.

Note that interpreted execution does not necessarily mean poor performance. Modern interpreters can use techniques such as bytecode optimization, caching, and JIT compilation to greatly improve execution efficiency.

IV. Core Differences Between Compiled and Interpreted Languages

Comparison Dimension Traditional Compiled Languages Traditional Interpreted Languages
Code conversion Usually compiled to machine code in advance Usually interpreted at runtime
Execution subject The CPU executes the generated machine instructions An interpreter or virtual machine
Build process Usually requires explicit compilation and linking Usually can be run directly
Runtime performance Usually offers high execution efficiency Traditional implementations may incur interpretation overhead
Platform dependency Machine code depends on the target platform Depends on a compatible interpreter or runtime
Error detection Some errors can be found during the compilation phase More errors may be exposed at runtime
Deployment method Executable files, dynamic libraries, etc. Source code, bytecode, and the runtime environment
Typical representatives C, C++, Rust, Go Python, traditional JavaScript implementations

1. Differences in Execution Performance

Traditional compiled languages can usually complete a great deal of work in advance:

  • Type checking and some semantic checking.
  • Constant folding.
  • Dead code elimination.
  • Loop optimization.
  • Instruction selection.
  • Register allocation.

After machine code is generated, the CPU can execute these instructions directly, without going through the instruction dispatch process of a general-purpose interpreter every time.

Traditional interpreted execution, by contrast, may continuously perform the following during runtime:

  • Bytecode reading.
  • Instruction decoding.
  • Operand fetching.
  • Type determination.
  • Runtime dispatch.
  • Dynamic method lookup.

These operations may incur additional overhead.

However, one cannot simply assume that compiled languages are always faster than interpreted languages. Actual performance also depends on:

  • Compiler optimization capabilities.
  • Runtime implementation.
  • Memory management approach.
  • Algorithms and data structures.
  • I/O and network waits.
  • Dynamic typing and reflection overhead.
  • JIT compilation and runtime optimization.

For example, the overall time of a Python program that performs a large number of database queries may be dominated by the database and the network rather than by the Python interpreter.

2. Differences in Cross-Platform Capability

Machine code generated by compiling the C language usually cannot be used directly across different CPU architectures.

For example:

Build output for the x86-64 platform
Build output for the ARM64 platform

The two usually need to be built separately.

A Python program, however, can run on different platforms, as long as the corresponding platform provides a compatible Python runtime environment and the dependency libraries also support that platform.

But this does not mean that interpreted languages have no platform limitations at all.

For example, if a Python project depends on system calls of a specific operating system, C extension modules, or native dynamic libraries, it may also need to be adapted for different platforms.

V. Why Do Modern Languages No Longer Strictly Distinguish Between Compiled and Interpreted?

The traditional classification easily leads people to form a mistaken belief:

Compiled languages only compile, and interpreted languages only interpret.

Modern language runtimes have already broken this simple division.

Many languages combine compilation, interpretation, and just-in-time compilation, choosing the more appropriate approach based on execution conditions.

1. Java: Compilation and Virtual Machine Execution

The execution process of Java usually includes:

flowchart TB
    A["Java 源代码 .java"] --> B["javac 编译"]
    B --> C["字节码 .class"]
    C --> D["JVM 加载"]
    D --> E["字节码解释执行"]
    E --> F["热点代码识别"]
    F --> G["JIT 编译"]
    G --> H["本地机器代码执行"]

    style A fill:#E5F4EC,stroke:#A3CBB5,color:#334155
    style B fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style C fill:#F3EDFC,stroke:#C4B2E3,color:#334155
    style D fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style E fill:#FFF5DE,stroke:#E8D29A,color:#334155
    style F fill:#E8EDF2,stroke:#B8C3CE,color:#334155
    style G fill:#F3EDFC,stroke:#C4B2E3,color:#334155
    style H fill:#E5F4EC,stroke:#A3CBB5,color:#334155
    linkStyle default stroke:#A3B8AC,stroke-width:1.5px

Detailed process:

  1. javac compiles Java source code into JVM bytecode.
  2. The JVM loads the bytecode and performs verification, linking, initialization, and other operations.
  3. The JVM can execute bytecode through an interpreter.
  4. The JVM identifies frequently executed hot code.
  5. The JIT compiler compiles hot code into native machine code.
  6. Subsequent execution can directly use the generated machine code.

This approach balances cross-platform capability with runtime performance optimization.

2. JIT Just-In-Time Compilation

JIT (Just-In-Time Compilation) means just-in-time compilation.

It is a technique that compiles certain intermediate code or program code into native machine code during program execution.

Unlike traditional ahead-of-time compilation (AOT), JIT can optimize using information collected at runtime.

For example:

  • A function is called frequently.
  • A particular branch remains dominant over time.
  • An object type is highly stable at a specific location.
  • A certain loop becomes a hot spot of the program.

JIT can perform targeted optimizations based on this runtime information.

Its typical flow is as follows: