DEV Community

Naval Kishor Upadhyay
Naval Kishor Upadhyay

Posted on

How Code Becomes Software: Execution Models Explained

How Code Becomes Software: Execution Models Explained

When we write code, we are creating text files in a programming language. For example, you may write print("Hello World") in Python or System.out.println("Hello World"); in Java. But computers do not understand these high-level commands.

A computer processor only understands machine code — a sequence of 0s and 1s that directly control the hardware. So, how does our human-readable code turn into running software?

The answer lies in execution models. An execution model describes how code is translated and executed by the computer. Different models exist, and each comes with its own strengths and weaknesses. Let’s explore them.


1. Source Code: The Starting Point

The journey begins with source code. This is the text written by developers in a programming language. Source code is easy for humans to read and write, but impossible for a processor to execute directly.

We need a way to transform this source code into something the processor can understand. This is where compilers, interpreters, and virtual machines play their role.


2. Compilation Model

In the compilation model, the program is completely translated into machine code before it is run.

  • How it works:

    1. You write source code in a language such as C.
    2. A compiler converts this code into a binary executable file.
    3. The operating system loads this file, and the processor executes it directly.
  • Example languages: C, C++

  • Advantages:

    • The resulting program is very fast because it is already in machine code.
    • Compilers can optimize the program to use resources efficiently.
  • Disadvantages:

    • You must recompile the program after every change.
    • Compiled code is usually platform-dependent. A Windows program will not run on Linux unless it is recompiled.

3. Interpretation Model

In the interpretation model, the code is not converted to machine code in advance. Instead, it is executed line by line by an interpreter.

  • How it works:

    1. You write code in a language like Python.
    2. When you run it, the interpreter reads each line and executes it immediately.
  • Example languages: Python, JavaScript, Ruby

  • Advantages:

    • You can run the program instantly without waiting for compilation.
    • The same code can usually run on any system with the interpreter installed.
  • Disadvantages:

    • Interpreted programs are slower because translation happens while the program runs.
    • They use more memory, since the interpreter stays active during execution.

4. Hybrid Model (Compile + Interpret)

Many modern languages use a hybrid model that combines both compilation and interpretation.

  • How it works:

    1. The source code is first compiled into an intermediate form called bytecode.
    2. This bytecode is run on a virtual machine (VM), such as the Java Virtual Machine (JVM) or the .NET Common Language Runtime (CLR).
    3. Inside the VM, a Just-In-Time (JIT) compiler may translate frequently used parts of the bytecode into machine code during execution.
  • Example languages: Java, C#, modern Python

What is JIT Compilation?

JIT stands for Just-In-Time compilation. Instead of compiling the entire program before execution, JIT compiles only the parts that are actually used a lot.

  • When you start the program, the VM begins by interpreting the bytecode.
  • The VM watches which parts of the code (called hotspots) run most often.
  • Once hotspots are detected, the JIT compiler translates them into machine code.
  • After that, the program reuses the compiled machine code, making execution much faster.

This means programs can start quickly (thanks to interpretation) and then speed up while running (thanks to JIT).

Example:

Imagine a Java program that sorts numbers in a loop one million times. At first, the loop runs slowly because it is interpreted. But once the VM sees that the loop is used repeatedly, it compiles the sorting function into machine code. From that point on, the function runs almost as fast as if it were written in C.

  • Advantages:

    • Good balance between portability and performance.
    • The program can optimize itself based on real usage.
  • Disadvantages:

    • Programs may take longer to start because the VM needs time to analyze and compile hotspots.
    • The execution model is more complex.

5. Ahead-of-Time (AOT) Compilation

Ahead-of-Time compilation is a variation where code is compiled into machine code before runtime, even in languages that usually depend on VMs.

  • How it works:
    Instead of waiting for the VM to JIT compile during execution, the program is compiled into native machine code in advance.

  • Example languages: Rust, Go, Kotlin/Native, and some .NET environments.

  • Advantages:

    • Programs start faster since they don’t need JIT during runtime.
    • Useful for mobile or embedded systems where resources are limited.
  • Disadvantages:

    • Less flexible compared to JIT, because the program cannot adapt itself at runtime.

6. Scripting vs Systems Languages

Traditionally, programming languages were divided into two groups:

  • Scripting languages (like Python, JavaScript): Usually interpreted, easy to learn and use, good for quick tasks and automation, but slower in execution.
  • Systems languages (like C, C++): Compiled, harder to learn, but fast and powerful, often used for building operating systems, drivers, or performance-heavy applications.

Today, the line between these two groups is blurred. For example, JavaScript engines like V8 use JIT compilation, making them much faster than traditional interpreters. And scripting languages often connect to compiled libraries for heavy tasks.


7. Why Execution Models Matter

Understanding execution models is important because they affect real-world decisions:

  • Performance: Games, databases, and operating systems often use compiled or AOT models for speed.
  • Portability: Enterprise software and cross-platform apps often use bytecode and VMs.
  • Development Speed: Interpreted languages are better for prototyping and experimentation.
  • Special Cases: Embedded devices or mobile apps may prefer AOT to reduce memory and improve startup time.

Closing Thoughts

When you press Run, a lot happens behind the scenes. Your code may be compiled into machine code, interpreted line by line, or optimized on the fly with JIT.

These execution models explain why C runs so fast, why Python is so flexible, and why Java can run almost anywhere.

In short, code is only the beginning. The execution model is what transforms it into living, working software.

Top comments (0)