Compiler

Compiled Language vs Interpreted Programming Languages

So we need to get our source code converted into machine code somehow before it can run and there are two main ways of doing this what’s called compiling the source code and what’s called interpreting the source code now luckily this is not a big decision you have to worry about most languages you’ll deal with will naturally fall into one or the other but it is worth knowing the difference so let’s have a simple scenario let’s say it’s just you and me you have your computer and I have my computer and you’re going to write a program that you want me to run now with a compiled language what happens is you write your source code and then you have a program called a compiler that will go through that source code and create a separate file that contains the machine code and you just give me that file this end result is sometimes referred to as an executable or an executive or file because I can directly execute it.

I can now just run your program you keep your source code and I never see it now with an interpreted language on the other hand you don’t compile your source code beforehand you just give me a copy of it so I’ll need my machine to interpret it whenever I want to run your program now interpreter is different to a compiler it does this on the fly we can think of it as going through your source code line by line and processing it on the spot it does not save it as a separate machine code file now you’ve used interpreted languages even if you don’t know it whenever you’ve looked at a web page with JavaScript which if you’ve surfed the web for more than two minutes in your life you have this is what’s been happening the JavaScript has been sent to you over the web along.

With a bunch of other files like webpages and images and it’s been sent as source code onto your machine and your web browser has just interpreted that JavaScript so it can run that code so which one’s best while they both have their good and their bad points benefits of compiled code once it’s compiled it’s immediately ready to run and you could send it to a hundred or a thousand or a hundred thousand different people it’s ready to go it can be optimized for a CPU so it can actually be faster and you don’t have to send your source code to everybody which might be a good thing however the downsides are if I compile it on a PC that executable file won’t work on a Mac in fact it often needs to be compiled separately for different kinds of CPU.

This image has an empty alt attribute; its file name is business-2042283_1920-1024x683.jpg

Even on the same platform and when you’re writing code to compile is an extra step that you have to take every time you want to test your program now with interpreted code the big benefits are I don’t really care what kind of machine is on the other end because we don’t provide machine code we just send the source code and we let the other side to take care of it so it can be more portable and more flexible across platforms it’s also a little easier when testing because you just write your source code and then run it letting the interpreter take care of converting it there is no in-between compile step and it can be easier to debug when things go wrong because you always have access to all the source.

Code however it has its downsides too because everyone who needs to run that program on their machine has to have an interpreter for that language on their machine it also can be slower because you have to interpret it every time the program is run and the source code is effectively public because you’re sending it to everyone who needs to run that program now because there are good things about compiled languages and good things about interpreted languages there’s also a third way of doing this which is a bit of both instead of the compiled model where all the work is done up front that can be a little bit inflexible or the interpreted model where all the work is done on the receiving end but can be a little bit slower we kind of do half and a half upfront we compile it part of the way to what’s called an intermediate language.

Which makes it as far along the way to machine code as it can get while still being portable often across platforms you then distribute this sending it to the people who need to run it and each person who runs it takes the last step to take it to machine code on their computers this is sometimes referred to as just-in-time or JIT compilation now this intermediate language sometimes also goes by the name of bytecode so this process has to happen somehow it’s just how much of it happens on your machine and how much of it happens on mine now while theoretically, all computer languages could use any of these methods the normal usage of any one language tends to be one or the other so, for example, C C++ and Objective C these are typically found as compiled languages so you need a compiler and the compiler can be downloaded for free but are often built into integrated development environment applications.

Now languages like PHP and JavaScript indeed most languages with the word script at the end are usually interpreted and languages like Javac-sharp vb.net Python use this intermediate hybrid approach now whether a language is compiled or interpreted or somewhere in between is rarely a reason by itself to choose a language but it can be something that you take into account if one main priority of your program is absolute maximum speed running on one single platform you’ll probably look at a compound language if you’re more interested in easily moving your code across multiple platforms you’re probably more interested in an interpreted one but more usually you’re driven more by what you need to do you need to build iPhone apps or Windows desktop apps or dynamic websites or in our case just learn the fundamentals of programming and you let that decision drive the language choice and the language choice will determine whether your compiled interpreted or somewhere in the middle.

Leave a Comment