Back to series
· 9 min read

Compiled vs. Interpreted Languages: A Practical Comparison


I. What Are Compiled Languages and Interpreted Languages?

Compiled languages and interpreted languages are two common categories of program execution models.

Their core difference lies in: at what stage and in what way a program is converted into instructions that the machine can execute.

1. Compiled Languages

Compiled languages usually use a compiler to convert source code into object code or machine code in advance, which is then loaded and executed by the operating system or a runtime.

Typical languages include:

  • C
  • C++
  • Rust
  • Go
  • Swift

For example, a piece of C code:

#include <stdio.h>

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

Compile with GCC:

gcc main.c -o main

Run the program:

./main

In a typical Linux x86-64 environment, GCC usually compiles, assembles, and links the source code into an ELF executable.

When the program runs, the operating system loads the executable file and executes the machine instructions within it.

Typical process:

flowchart TD
    A["Source Code"] --> B["Compiler"]
    B --> C["Object File"]
    C --> D["Linker"]
    D --> E["Executable File"]
    E --> F["Operating System Loads"]
    F --> G["CPU Executes Machine Instructions"]

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

Typical characteristics of compiled languages:

  • Usually, the main compilation work is completed before the program runs.
  • Can generate native machine code or other object code.
  • Some syntax and type errors can be found during the compilation phase.
  • The compiled output can be deployed independently, and may also depend on dynamic libraries or a runtime.
  • In many scenarios, it offers high execution performance.

Note: A compiled language does not necessarily produce a completely standalone executable, nor does it mean that no support is needed at runtime.

2. Interpreted Languages

Interpreted languages usually use an interpreter to read program code or an intermediate representation and execute the program logic at runtime.

Typical languages include:

  • Python
  • Ruby
  • PHP
  • Perl
  • JavaScript (under the traditional interpreted execution model)

For example, Python code:

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

result = calculate(10, 20)
print(result)

Run:

python main.py

In CPython, the execution process is not simply reading the source code line by line and executing it directly.

CPython usually first compiles the source code into bytecode, and then the Python virtual machine executes the bytecode.

Typical process:

flowchart TD
    A["Python Source Code"] --> B["Lexical and Syntax Analysis"]
    B --> C["Generate Bytecode"]
    C --> D["Python Virtual Machine"]
    D --> E["Execute Bytecode"]
    E --> F["Result"]

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

Typical characteristics of interpreted languages:

  • Usually depends on an interpreter or virtual machine for execution.
  • During development, source code or scripts can be run directly.
  • Some errors are exposed only when execution reaches the corresponding code path.
  • Usually has more flexible runtime behavior.
  • Execution performance depends on the interpreter, runtime optimizations, and the program itself.

Being an interpreted language does not mean there is no compilation process. Languages such as Python and JavaScript may generate intermediate representations, bytecode, or even machine code before or during execution.

II. Core Differences Between Compilation and Interpretation

From the perspective of execution mechanisms, the differences between the two are mainly reflected in the timing of code transformation, the execution medium, and runtime behavior.

Comparison Dimension Compiled Languages Interpreted Languages
Typical Execution Method Compile ahead of time, then execute Interpreted execution at runtime
Code Transformation Usually completed ahead of time Part of the transformation is usually completed at runtime
Execution Medium Executables, runtime, etc. Interpreters, virtual machines, etc.
Error Detection Some errors are found during compilation Some errors are found during execution
Deployment Method Commonly deploy compiled artifacts Commonly deploy source code and the runtime environment
Startup Overhead May be lower, but is also affected by the runtime May include interpreter initialization overhead
Runtime Performance Usually offers more room for optimization Depends on interpretation and runtime optimization capabilities
Platform Adaptation May require compilation for the target platform Usually requires an interpreter for the corresponding platform

Two concepts need to be distinguished here:

Compiled and interpreted mainly describe execution mechanisms, not an immutable property of the language itself.

The same language can run through different implementations.

For example:

  • C can be compiled into native machine code, or executed through an interpreter or JIT environment.
  • Python can interpret bytecode through CPython, and implementations with JIT such as PyPy also exist.
  • JavaScript can be interpreted, or hot code can be optimized through JIT compilation.

Therefore, the actual execution method used by a program cannot be judged solely by the language name.

III. Why Are Compiled Languages Usually Faster?

Compiled languages often have performance advantages, but this is not an absolute rule.

The main reasons include the following aspects.

1. Generating Machine Code Ahead of Time

The compiler can convert source code into instructions for the target machine before the program runs.

For example:

int add(int a, int b) {
    return a + b;
}

The compiler can convert the addition operation into the corresponding machine instructions according to the target CPU's instruction set.

At runtime, it does not need to analyze the semantics of the original code through an interpreter every time.

This reduces some runtime conversion and scheduling overhead.

2. Compile-Time Optimization

The compiler can perform many optimizations during the compilation phase.

Common optimizations include:

Optimization Technique Effect
Constant folding Compute expressions that can be determined at compile time in advance
Constant propagation Replace variables with known constants
Dead code elimination Remove code that does not affect the program result
Loop optimization Reduce repeated computation in loops
Function inlining Replace function calls with function bodies
Common subexpression elimination Avoid repeatedly computing the same expression
Register allocation Use CPU registers to store data as much as possible

For example:

int calculate() {
    int a = 10;
    int b = 20;
    return a + b;
}

When the compiler confirms that the semantics allow it, it can determine the computation result as a constant in advance.

The actually generated machine code may no longer retain all the variables and operations from the original code.

3. Reducing Dynamic Runtime Overhead

Some interpreters need to complete the following during execution:

  • Bytecode decoding.
  • Operand stack management.
  • Dynamic type checking.
  • Object access.
  • Dynamic method lookup.
  • Runtime error checking.

These operations may increase execution overhead.

In contrast, statically compiled languages can usually determine more type and call information at compile time, thereby reducing some dynamic processing costs.

However, modern runtimes can also reduce these overheads through inline caching, type feedback, and JIT optimization.

IV. JIT: A Bridge Between Compilation and Interpretation

JIT (Just-In-Time Compilation) is an important concept for understanding modern language execution mechanisms.

It is not simple interpreted execution, nor traditional fully ahead-of-time compilation; rather, during program execution, it compiles part of the code into machine code based on actual execution conditions.

1. Basic Principles of JIT

Take the Java Virtual Machine as an example:

Java source code is usually first compiled into bytecode, which is then loaded and executed by the JVM.

The JVM can start the program through interpreted execution and collect runtime information such as method invocation counts and branch behavior.

For frequently executed hot code, the JIT compiler can compile it into native machine code.

flowchart TD
    A["Java source code"] --> B["javac compiler"]
    B --> C["Bytecode Class"]
    C --> D["JVM loading"]
    D --> E["Interpreted execution"]
    E --> F["Collect runtime information"]
    F --> G{"Is it hot code?"}
    G -->|No| E
    G -->|Yes| H["JIT compilation"]
    H --> I["Native machine code"]
    I --> J["Execute optimized code"]

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

This means:

  • When the program first starts, it may rely mainly on interpreted execution.
  • As runtime increases, hot code is gradually identified.
  • Hot code can be compiled into machine code.
  • Subsequent execution can directly use the optimized machine code.

2. Advantages of JIT

JIT can not only generate machine code, but also use runtime information for dynamic optimization.

For example:

Type specialization

If a function consistently receives integers, JIT can optimize the execution path for that actual type.

Inline caching

If access to an object property or method consistently points to the same structure, the runtime can cache access information and reduce repeated lookups.

Hot code optimization

JIT can treat frequently executed loops or functions as key optimization targets.

Dynamic deoptimization

If previous type assumptions no longer hold, the runtime may discard optimized code and revert to a more general execution path.

3. Limitations of JIT

JIT also has its own costs:

  • Initial compilation takes extra time.
  • Memory is needed to store machine code and runtime information.
  • The compiler itself consumes CPU resources.
  • Runtime optimization may increase system complexity.
  • For programs with short execution times, the benefits of JIT optimization may not be obvious.

Therefore, JIT does not guarantee that any program will run faster than interpreted execution.

5. Differences Between AOT and JIT

AOT (Ahead-Of-Time Compilation) and JIT are two different compilation timings.

Comparison dimension AOT JIT
Compilation timing Before program execution During program execution
Basis for compilation Source code, static information, and compilation configuration Source code or intermediate representation, and runtime information
Startup cost Usually no runtime compilation of hot code is required May incur runtime compilation overhead
Basis for optimization Mainly relies on information available at compile time Can use real runtime data
Runtime dependency Depends on the artifact and the language runtime Usually requires a JIT runtime environment
Typical applications C, C++, Rust, Go JVM, modern JavaScript engines

Note:

  • AOT does not necessarily mean there is no runtime.
  • JIT does not necessarily mean the entire program is executed through an interpreter.
  • Some languages and platforms support both AOT and JIT.
  • AOT and JIT can also work together in the same runtime environment.

For example, the Java ecosystem includes both traditional JVM JIT and AOT solutions such as GraalVM Native Image.

6. Actual Execution Mechanisms of Mainstream Languages

Classifying languages as compiled or interpreted based solely on their names can easily overlook differences between implementations.

The following comparison is based on actual runtime mechanisms.

1. C / C++

C and C++ usually use AOT compilation.

Typical process:

flowchart TD
    A["C / C++ source code"] --> B["Compiler"]
    B --> C["Assembly code or object files"]
    C --> D["Linker"]
    D --> E["Native executable"]
    E --> F["Operating system loading"]
    F --> G["CPU execution"]

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

Characteristics:

  • Usually generates machine code for the target platform.
  • Can perform relatively deep compile-time optimization.
  • Runtime overhead is relatively manageable.
  • Dependencies can be organized through static or dynamic linking.
  • Usually needs to be built for different CPU architectures and operating systems.

2. Rust

Rust typically uses AOT compilation.

The Rust compiler performs type checking, borrow checking, optimization, and code generation.

Characteristics:

  • Strict type and ownership checks are performed at compile time.
  • Usually generates native machine code.
  • Does not require a traditional garbage collector.
  • Can cross-compile for other platforms.
  • Runtime support is usually lightweight, but runtime dependencies are not entirely absent.

Rust’s performance advantages come mainly from compile-time optimization, its memory management model, and low abstraction overhead, not simply from the fact that it is a compiled language.

3. Go

Go typically uses AOT compilation.

The Go compiler converts source code into machine code for the target platform and produces an executable program.

Characteristics:

  • The compilation process is relatively straightforward.
  • Usually generates a native executable.
  • Has a built-in garbage collector.
  • Has a goroutine scheduler.
  • Can produce programs for other target platforms through cross-compilation.

Although Go is a compiled language, its programs still include runtime components such as the garbage collector and scheduler at runtime.

4. Python

Taking CPython as an example, Python typically uses a model of bytecode plus virtual machine interpretation.

Characteristics:

  • Source code is usually first converted to bytecode.
  • Bytecode is executed by the Python virtual machine.
  • Dynamic typing provides considerable runtime flexibility.
  • Common implementations depend on a Python runtime environment.
  • Implementations such as PyPy can use JIT optimization.

Therefore, Python cannot simply be understood as interpreting source code line by line.

5. Java

Java typically uses a combination of AOT compilation and runtime JIT.

Characteristics:

  • javac compiles source code into bytecode.
  • The JVM loads, verifies, and executes bytecode.
  • It can interpret bytecode.
  • JIT can compile hot code into machine code.
  • The garbage collector handles automatic memory management.

Java’s portability comes mainly from bytecode and the JVM, not from the idea that Java bytecode can be executed directly by the CPU without a runtime environment.

6. JavaScript

Modern JavaScript engines typically combine interpretation, baseline compilation, and optimizing JIT.

For example, the V8 engine contains multiple execution and optimization tiers.

Characteristics:

  • It can run JavaScript source code directly.
  • The engine usually generates an internal representation or bytecode.
  • It can optimize hot code based on runtime information.
  • It supports dynamic types and dynamic object structures.
  • Runtime optimization may involve deoptimization and recompilation.

Therefore, modern JavaScript engines are not simple line-by-line interpreters in the traditional sense.

7. Summary of Language Execution Mechanisms