Interpreter vs Commpiler

Beginner’s Guide to Programming – Interpreters vs Compilers

In our last tutorial we looked at how to install Python and how to start it in our command line. When we opened up Python in our command line, I called it an interpreter. In today’s lesson we will look at what an interpreter is, what a compiler is, and we’ll understand the difference between the two.

We won’t be writing any code in this tutorial, so you’re more than welcome to skip ahead. If all you want to do is start coding, this article will be pretty boring. You can always come back later if you want to know about interpreters and compilers. I won’t judge. For those that want to stick around, carry on.

Topics Covered 

  • Recap: What is a program?
  • What is a compiler?
  • What is an interpreter?
  • Interpreter VS Compiler

If you have read the very first post in the series titled, Introduction to Programming for Beginners, you might remember that I brought up the term compiler. It’s OK if you forget, or skipped the first lesson. We’re going to recap, and focus a little more on what a compiler is. This will lead us into understanding what an interpreter is, and why it matters for Python.

Recap: What is a Program?

To understand what a compiler is lets recap on what a computer program is. We went over this in great detail in this article: Introduction to Programming For Beginners. Basically a program is a set of instructions that a computer can understand. It just so happens to be that computers today have been built in a way that they understand 1’s and 0’s. We call this language binary, or machine code.

Understanding this concept is important, because the modern form of programming is not done with 1’s and 0’s. In today’s world, most programmers write code that looks much closer to written languages like English.

You saw this in our last tutorial when we wrote the following:

print('hello world')

The code above is easily understood by humans but it can’t be understood by a computer. At least not directly. That’s were a compiler comes in.

A long time ago programs were written in 1’s and 0’s, and someone got really sick of it. Her name was Grace Hopper, and she decided to write a program in 1’s and 0’s that could translate other simpler forms of code into 1’s and 0’s that the computer could understand.

This program allowed Hopper to write shorter codes to perform common tasks, instead of writing out all of the code in binary. This program was called a compiler.

So what is a compiler?

Strictly speaking a compiler is a program that translates high-level source code into machine code that can later be executed by a computer.

Source code is the name we give to code that has not yet been translated into binary. Sometimes a file containing source code is referred to as a script.

To understand what I mean by “high-level”, know that low-level languages are ones that are closer to what the computer can understand. The lowest level being machine code.

The compiler that Grace Hopper wrote, translated a source code known as Assembly Language. Assembly is very close to machine code and is considered low-level. When I say that it’s “close” to machine code I mean that each code in assembly represents one code in binary. The advantage was that the codes themselves were shorter, even though the number of instructions were still the same.

Since the first assembly language, there have been many assembly languages, and those languages have been used to create better compilers. A compiler is what is used to create a new higher-level programming language. In a high-level language a single instruction can get translated into many machine instructions. So it’s not just that high-level languages look more like English, each instruction is also doing a lot more work than a low-level instruction.

Side Note: Most programs today are written in high-level languages like C++, C#, Java, or Python. But there are still uses for assembly languages. Video games and other high performance programs still use at least some assembly in their code.

Today’s compilers take files containing source code and compile them into machine code. The output of a compiler is what you may know as an executable, or a file with a .exe extension. An executable contains only 1’s and 0’s that your computer can understand. Another term for code inside of an executable is object code. In fact that’s what we normally call code that has been translated from source code into machine executable code.

The key point to remember about a compiler is that your source code does not get executed by the computer until after compilation. What I mean is that when you are writing code in a language that uses a compiler, and then you try to run the code, it doesn’t start running until every line of code has been turned into object code.

This is very different from how Python and other interpreted languages get translated, as you’re about to find out.

What’s an interpreter?

As we discussed earlier an interpreter is what Python uses. An interpreter serves the exact same purpose as a compiler. It is a program that translates high-level code into 1’s and 0’s that the computer can understand.

What an interpreter does is it looks at a line of code in your script (file with source code in it) and translates that line so the computer can execute it. It then immediately sends that translated instruction to the computer to execute before moving on to the next line. We call this process interpretation.

This means that your code begins running before it gets fully translated. Each line in your code is interpreted one at a time. There are pros and cons to this method as we’ll soon look at, but there are also many pros and cons to compilers as well. Let’s look at the differences between interpreters vs compilers.

Interpreters vs Compilers

One advantage of using an interpreted language like Python is that you can get instant feed back as you write your code.

In the last tutorial we wrote a “hello world” program that executed instantly. We typed a command into the interpreter, and it gave us back the results of that command right away.

In a compiled language you first have to compile before running your code. This means you have to wait for compilation to complete before you can run your program. This wait time can be quite long if you are building a big enough application.

One advantage of compiled languages is that the programs made by them usually run faster, and are much more efficient. This is because the compiler is never running on the computer while the program is being executed. By the time the program is ready for execution, there is no need for the compiler to use any resources. In fact there’s no need for the compiler to even be installed on the computer at all. The computer only needs a compatible executable, that’s it.

This is the reason compiled languages such as C++ are used so heavily in AAA game development. Video games are extremely performance intensive and require all of the computer’s resources that are possible. Running an interpreter in the background would hurt the performance of the game.

And this is one of the disadvantages of using an interpreted language like Python. The interpreter is always running in the background, meaning that it is using up some of the computers processing power and memory. This typically means that programs running on an interpreter perform worse than programs that have been compiled.

As you gain experience as a programmer, you will learn that neither system is better than the other. There are appropriate situations for both compilers and interpreters and many programming environments use some combination of the two.

One last difference I’ll mention is that interpreted languages tend to be more portable than compiled ones. This means that when you write a program on one machine like a Windows PC, it takes less effort to get that same code working on a Mac or a Linux machine. Portability is a big advantage of interpreters.

Conclusion

I can get a lot more in depth about this subject, but I’m trying to keep it beginner friendly. I’m hoping that my explanation is comprehensive enough so that any beginner does not feel like details are missing, but still simple enough so that they don’t feel overwhelmed.

What you need to know moving forward to the next tutorial is that when we installed Python, we installed the Python interpreter. When you double click python.exe what you’re doing is running the interpreter. The same happens when you type python into your command line.

Once the interpreter is running, it sits there idling waiting for you to give it instructions. There are two ways of giving the interpreter instructions and we’ll learn about both of them in the next tutorial.


Feel free to ask questions in the comments below, I’d love to help you out.

Also, if you spot any errors in this tutorial please let me know!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.