In this tutorial, we’ll present the difference between compiled and interpreted programming languages.
2. Why Do We Need Compilers and Interpreters?
Computers can understand and run only binary code. Programmers use high–level programming languages, such as C, Python, or Java. Those languages are easier to work with since they resemble human languages and mathematical notation. However, computers cannot run the code written in a high–level language. We first have to translate it into binary code. To do so, we use compilers and interpreters.
Languages whose programs we usually compile are called compiled languages. Similarly, those we usually interpret are called interpreted languages.
Compilers take a whole program as input and translate it to an executable binary code in several steps.
We can run the binary code only on the machine on which we compiled it. That’s because the binary code depends on the hardware and is not portable.
The compilation step is required only once. Afterward, we can run the binary code as many times as we want.
Because compilers process whole programs, they can catch some errors and warn us to correct them. Those are syntax and type errors. Compilation fails if they are present.
C is an example of a compiled language.
Interpreters read and execute the program at hand instruction by instruction. After being read, each instruction is translated into the machine’s binary code and run.
Unlike compilers, the interpreters do not produce a binary executable file. Each time we run a program, we invoke the interpreter. It then reads and executes the program one instruction at a time.
That’s why it must be present in the computer’s RAM whenever we run a program. In contrast to interpreters, we need compilers only during compilation.
On the other hand, unlike the compilers, the interpreters catch all the errors at runtime.
Python is an example of an interpreted language.
5. Example of Compilation and Interpretation
Imagine a code in an unnamed language:
a := 1;
b := 2;
c := a + b;
A compiler would translate it to the binary code, which we can run later:
All those binary instructions are the commands for the computer at hand. They instruct it to set the appropriate CPU register to 1. Afterward, they choose a free address in RAM to write the register’s content. That is how a compiler translates a := 1; to the binary code. Other binary instructions are translations of the rest of the code to the machine’s binary language.
Once we run the binary code, it gets loaded into RAM, and the CPUs execute it.
On the other hand, an interpreter would first read the instruction a := 1;. It would load it into the computer’s RAM, translate it into binary code and let the CPU(s) execute it. Then, the interpreter would do the same with instruction b := 2;. After the second instruction, it would move on to the third, and finally, to the last one:
So, we see that translation and execution are separated if we use compilers. However, they are intertwined if we interpret our programs. That’s why interpretation is usually slower than running compiled programs.
Let’s sum up the differences between compilers and interpreters:
In this article, we talked about executing programs after compilation or during interpretation. We also talked about the differences between those two ways of execution.