Interpreter vs. Compiler
Photo by Blake Connally on Unplash |
Introduction
Developers usually build apps by writing code in a high-level language that is only understandable by humans. Meanwhile, the computer has its own language so before a computer program can be executed, human code needs to be converted to computer-readable machine code, and here comes the appearance of interpreters and compilers.
Interpreters and compilers are both translators but they work in very different ways.
Interpreter
The interpreter translates and then executes the code immediately line by line. Therefore, we always need an interpreter on standby while the program runs.
Figure 1: How the interpreter works |
Figure 1 shows the interpreter taking a single line of code and translating this into immediate code before executing. It then gets the next line and repeats the process until the end of the source code.
Compiler
In contrast, the compiler takes the whole source code, translates then saves it as an executable file. Since then, the compiler leaves us there and the program can be executed on demand.
Figure 2: How the compiler works |
Figure 2 shows the translation process taken by the compiler happens once. Besides, it does not include the code execution part since the compiler does not involve in this process.
Interpreter vs. Compiler
The table below shows the differences in brief between these translators:
Interpreter | Compiler | |
---|---|---|
How it translates the code | Line by line. | All at once. |
How the program runs | Translates and then executes the code line by line. | translates the whole source code and then saves it as files that can be executed by users. |
How long the whole process takes? | Usually slower. | Faster. |
Temporary storage | Not required. | Required to save the translated code. |
Debugging | Quite easy since each line of code is run at a time. | Difficult when all errors in the source code are reported altogether. |
Programming languages | Javascript, PHP, Python | Java, C, C++, Golang |