Compilers and Interpreters

Compilers and Interpreters

Compilers first convert a programming language into machine code before it is run. The user of the software runs the compiled machine code and does not ever see the source code.

Interpreters run a program directly from the source code without first converting it into machine code. In order to run the program the user must have a copy of the interpreter available on their machine to process the source code.

Compiled languages, like C and Rust, are normally faster than interpreted languages, such as Python, as the source code has been converted up-front into language that the computer can understand whereas an interpreter must do this as it goes along. Every time that the program is executed the interpreter must again perform the conversion of the source code into machine language.

Compiled languages do not require the user to install any interpreter on their machine in order to run the software. Compiled software is naturally in a form where it can directly run. The extra step of requiring installation of other software before a program is run can dissuade users. It is also possible installation of the interpreter software can incur costs or licensing terms that are not aggreeable to the software developer or user.

Even though interpreted languages are slower than compiled languages they are still widely used. Running a compiler takes time. It may take a few seconds to several minutes to compile a program depending on how big it is. This means that every time a programmer wants to test their software they have to wait for the compiler to complete. This frequently encountered delay can make developing compiled software a nuisance. Developing interpreted software can be a much faster process as there is no such delay.

Compiled software is usually targeted for one specific type of machine and operating system. It cannot run on any machine type other than the one for which it was compiled. Interpreted software can often run on many types of machine. If there is an interpreter available for the target platform then the software will normally run. This makes the software more portable across many systems. The programmer can write a program once and release it immediately on all platforms for which there exists an interpreter.

With interpreted software the user is always supplied with a copy of the source code as this is intrinsically required to run the code. This can have benefits as the user is then able to modify the software as they wish. However, developers of proprietary or paid-for software can prefer that user does not receive a copy of the source code as they may wish to keep it hidden for their own commercial reasons to prevent others discovering how it works. For these reasons they may prefer compiled software as the compilation process naturally obfuscates the source code and makes it difficult to understand through the process of conversion to machine code.

There are some languages that use a combination of the two approaches. Java is an example of a language that does this. Java uses a compiler but it does not convert the source code into a directly executable form. The Java compiler converts the source code into an intermediate form called byte code. This is like machine code but there is no CPU that can run it. To run the Java byte code a program somewhat like an interpreter is used. The program does not however exactly interpret the code. Instead it compiles it as it goes along from the intermediate byte code form into machine code. This is known as ‘just-in-time’ compilation. The compiled version is thrown away once the program completes. The next time the program is run, it is just-in-time compiled again.

A Java program initially runs quite slowly whilst the compilation is proceeding. However the speed gradually improves as more and more of the program is translated into machine code. Eventually, all of the program has been translated into machine code and the program runs at full speed.

Java has the benefit of being portable as, like an interpreter, the just-in-time compiler can be made available for many systems but yet has the advantage that it can run at the speed of a compiled program. However, it has the disadvantage that it initially runs slowly whilst the just in time compilation takes place. The performance of the software can be unpredictable in the initial stages of execution which can make this approach unsuitable for some applications.