Interpreters

Challenge 7-1: Introduction to Compilers

Remember our media stand scenario from the introduction? Click here if you need a refresher.

Imagine that once again you've got a new media stand to build and a set of instructions you don't understand. Now imagine that this time your friend is sitting there with you, and every time you finish a step they translate the next one for you. This means you never get the fully translated manual but you are still able to get the job done just with single instructions given at a time. This time your friend would no longer be a compiler, now they are an interpreter.

These interpreters are different from compilers as they only start to translate when the program starts running. This means that every time a new section of code is needed it translates the code and then passes it to the computer. Python is an example of an interpreted language, while C++ is an example of a compiled language.

Interpreters have a few advantages over compilers, but they also fall short in some areas. Here are some of the key differences:

Pros

As interpreters only translate line by line it means there is no program build at the start, this means you can start using the program straight away. Compilers sometimes require minutes or even hours to build a large program before it can even be run!

Another bonus is that because an interpreter goes line by line you can change the program as you go without having to start the program again - useful, right? Compilers need to rebuild the program every time a change is made, and if it’s a big file that can definitely take a while!

Cons

Interpreters are not perfect though. As they use an external program to translate and do it line by line it can slow down the running of your code. Compilers do need a minute to build the code but once it is running it is much faster.

Although interpreters are flexible and dynamic when it comes to changing the code, they don’t have much control over anything else. Compilers are able to optimise the CPU and memory allocation which again can make a big difference if you are using a large file and every byte matters.