Compiler vs Interpreter – Programming Foundations #03

Here’s what you need to know about the compiler vs. interpreter choices you’ll need to make. By now, you already know that source code needs to be converted into machine code before it can be run by the computer. This is because at its most basic level, a computer can only understand strings of numbers that specify different operational codes. Writing code at this most basic level can get ridiculously complicated, and so today we use programming languages to make our nerdy little lives a lot easier.

But before you jump into the actual programming, you’ll first need to learn about the difference between compiler and interpreter.

programming-languages

 

Because the programming languages devised by humans are only understood by humans, the source code we write must be translated into machine code before the computer can execute it. How the programming language is translated into machine code depends on whether the source code language is a Compiled language or an Interpreted one.

Compiler or Interpreter? Compiling Source Code or Interpreting Source Code?

When it comes to the compiler vs. interpreter choice, you’ll find they both achieve their goal, just by using different means. Let’s take a look at the difference between compiler and interpreter and what see they each can do.

Compiled Languages (C / C++/ Objective-C)

If you’re using a compiled language like C or C++, you will write your source code and then use a Compiler. A compiler goes through your program/source code and then creates a file (sometimes called object file or executable) that contains your newly translated machine code that can be processed by the computer.

You can now send your program (executable file) to anyone you wish to share it with and they will be able to run your program directly on their computer. Also, you get to keep your source code to yourself without sharing it with the users of your program.

complied languages

Now you know that when using compiled languages, the source code must be converted into machine language before the program can be executed.  This is rather different from how Interpreted languages work. Let’s take a look below.

Interpreted Languages (Perl, PHP, JavaScript)

When using an interpreted language, you will not be converting the source code into machine language beforehand. Instead, you will be sharing a copy of your source code to the users of your program, and their computers will interpret the code when running your program.

So instead of translating the source code into an executable before running it, an interpreter converts the source code into executable machine language on the spot while the program runs.

An interpreter translates source code into machine code, combining it with runtime libraries before executing the code instantly during the program runtime. (A runtime library, or RTL is a collection of low-level software used to activate functions and services that are native to the computer on which the program is being run).

javascript
javascript

Compiling source code or interpreting it? What’s the difference?

Time to learn about the difference between compiler and interpreter.

So… compiler vs. interpreter. What’s the deal really? With interpreters, there will be no new files containing machine code created. Unlike a compiler, an interpreter will work on the fly, going through your source code instruction by instruction and handling instant execution.

PROs and CONs?

Check out the pros and cons of using a compiler vs. interpreter below.

Compiled languages – Advantages

  • Once the source code is translated to machine code, the file can be run instantly and be used by any number of people.
  • There is no on-the-fly conversion, so compiled languages typically have a faster runtime.
  • You get to keep your source code to yourself.

Compiled languages – Disadvantages

  • Compiled languages don’t work the same across different kinds of platforms (an executable created on your Mac will not work on a PC).
  • Compiled languages offer less flexibility than interpreted languages, as you sometimes have to compile code differently for different kinds of computers even if they are of the same platform.
  • You have to compile your code every time you revise it before you can test it.

Interpreted languages – Advantages

  • Code written in interpreted languages will work the same across all kinds of platforms.
  • No compiling needs to be done as conversion is done on the fly. This makes interpreted languages more flexible than compiled ones. You also don’t have to re-compile your code every time you revise it.
  • Can be easier to debug as you have control of your source code.

Interpreted languages – Disadvantages

  • The program cannot be run by the user unless they have an interpreter.
  • Program must be interpreted every time it is used, increasing program runtime.
  • Your source code will be available to all users of your program.

programming-languages

So what language should you use?

When it comes to the battle of compiler vs. interpreter, you can see that there are decided advantages in using either language type. But your choice of programming language is likely to depend more on the type of software you need created than on the language used instead.

For instance if you need to create an application that needs to work at top-speed while only running on one platform, then a compiled language would be a good choice. And if your software needs to work on multiple platforms easily, then an interpreted language would be the way to go. Also check out this list of the most relevant programming languages you need to start learning.

Now that you know about the difference between compiler and interpreter, you can make an educated choice when picking a suitable programming language for any task.

Intermediate Approach (Java, C#, VB.NET, Python)

You know that compiled and interpreted languages come with their own sets of pros and cons. But what if there was a third way beside the whole compiler vs interpreter drama? A way you could harness the benefits of both forms of languages with few disadvantages?

Consider the intermediate approach. Unlike compiled languages where all the work is done before running the program, and unlike interpreted languages where all the work is done on the end of the user, the intermediate approach allows your program to be compiled half-way (to Intermediate language) before being sent off to the user’s PC where the rest of the work is done.

python programming
compiler vs interpreter. python programming

This intermediate language (also called Byte Code) is as close to machine language as it can possibly be while still being available to work across different platforms. Once your program has reached the user computer, they now only have to convert the rest of the file into machine code before being able to run it.

In the next article we will be discussing all about using proper syntax in programming. Then we will be moving on to more programming foundations by mastering JavaScript basics.

While you’re here feel free to check out our other programming tutorials. We have tutorials on Python, Java and other coding tidbits you’ll love!

Leave a Reply

Your email address will not be published. Required fields are marked *

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