Compiler vs Interpreter Summary
When you talk to a friend on the phone using the same language, you can say whatever you want without needing someone to translate for you. But when you need to communicate with someone whose language you do not know (and who does not know your language), you need a translator.
In computing, we need “translators” to help us communicate with computers. This is because computers communicate in machine language. Programming languages exist as methods for communicating with computers. They are also called “high-level” languages, as opposed to a computer’s “low-level” language. When we “speak” a programming language to a computer, we need a translator to take that programming language and turn it into machine vernacular.
Fortunately, there are two types of translators that we can use to help us communicate with computers. These are the compiler and the interpreter. They perform essentially the same function but go about it in different ways. A compiler takes the program you have written and runs it all at once after you have written the entire program. An interpreter, on the other hand, runs each of your commands one line of code at a time.
Interpreter vs Compiler: How It Works
Let’s take a look at a couple of examples of how compiling and interpreting work.
The programming language C is called a “compiled language” because it uses a compiler. Its compilation process has several steps:
- First, the source code file is run through a preprocessor, which prepares the code for running through the compiler.
- Second, the code runs through a compiler (yes, there is something called a “compiler” within the larger compiler), which turns the code into assembly code.
- Third, the assembly code is passed through an assembler, which converts the code into pure binary code (also called “object code” or “machine language”).
- The code is then passed to a linker, which combines all the code files for your project and transforms them into a single executable file. This file contains instructions for the computer it’s being run on but does not include the original source code.
By contrast, JavaScript is called an “interpreted language.” Here’s what happens when you decide to run JavaScript code:
- You can run JavaScript code either in your browser or in your terminal via Node.js.
- Your code is then passed to a JavaScript engine. There are several JavaScript engines, including V8, Spidermonkey, JavaScript Core, and Rhino. Which engine you are using depends on which browser you have. For example, V8 is Chrome’s JavaScript engine. Node.js runs on the V8 engine.
- The JavaScript engine interprets the code, converts it into machine code, and then your computer can execute the commands. The JavaScript engine, sometimes referred to as a “virtual machine”, functions as an interpreter, which is why JavaScript is considered an interpreted language.
Note that both processes result in your source code being converted into machine code.
Programming languages tend to be classified as being either compiled or interpreted language. These terms describe how source code written in these languages is translated into machine code. Python, PHP, Ruby, Perl, and JavaScript are generally considered to be interpreted languages. C, C++, C#, and Rust are compiled languages.
Interpreter vs. Compiler: Pros and Cons
Both interpreters and compilers have advantages and disadvantages:
Speed
An interpreter tends to run more slowly than a compiled program because it reads each line one at a time. A compiled program takes time to compile initially, but once it is compiled into an executable, it can be run without being compiled again. This means that if you write a software program using a compiler and send the executable to your friend, the program will run more quickly than if they had to run it as an interpreted program.
Debugging
If you are using an interpreter, it is relatively easy to detect and fix problems in your code as you go. When you run the program, you will see every line that was executed prior to the error. You can clearly see which line contained the error because you will get an error message.
On the other hand, using a compiler can make errors more difficult to detect. The compiler runs the entire program as a whole and does not stop, even when there is an error. This could cause serious problems, like your computer system shutting down. It also means that you will not be able to easily see where the error was and so will have to spend more time debugging.
Source Code Visibility
With compiled languages, you can keep your source code private. An executable does not contain your source code, so when you share your program with others, they won’t see your “secret sauce.” With an interpreted language, anyone you share the program with will see your source code. If you work for a software company, having your source code visible could make it vulnerable to attackers or competitors who want to steal this proprietary content.
Compiler vs Interpreter: Is One Better Than the Other?
You may be wondering, “Does it matter whether I’m using a compiler or an interpreter?”
Your goals for your program can help determine whether you want to use an interpreter or a compiler. In turn, this can help you decide which programming languages are best suited for your project.
As you’re designing your program and development process, you’ll want to consider factors like speed, debugging, and source code visibility. If you’re building proprietary software to sell to consumers and you don’t want anyone else to use the source code, it could be a good idea to write the program in a compiled language. When someone uses an executable file (the end result of the compilation process), they can’t “decompile” the file to see the original source code. However, if you want to minimize the time you spend debugging, then you may want to write your program in an interpreted language.
Of course, there are other factors to consider when deciding how to write software. Different programming languages are most suitable for different kinds of tasks and have varying functionalities.
Both compilers and interpreters help software developers translate human-readable code into machine code. Compilers protect the visibility of source code and produce fast-running compiled programs. Interpreters allow developers to quickly debug programs, which saves time in the development process. Understanding what a compiler and an interpreter are and how they affect your development process is an important part of your work as a programmer.
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.