What is the difference between Compiler and Interpreter?
In my early days, when I had asked, how should I learn computer science and do something innovative? I always had come across, start learning computer languages. In hustle and bustle, many forget, how does actually it work? How does computer program run?
If you are also looking for same, you are reading a perfect article. I am sure, at the end of the post, you will get very well understanding over, what is a compiler? what is an interpreter? and what is the difference between compiler and interpreter?
I start with some brief prerequisite introduction. Basically, from human and machine point of view, there are high-level languages and machine level languages.
High-level language: It is written by the human (programmer). It is mostly like English type and can not be understood by the machine.
Machine level language: As Computer knows only 0 and 1 (on/off signal), a high-level language is not understood by the computer. So there is machine level language.
To run any program on the computer, you need some translator that will convert high-level language to machine level language. This translator is of two types compiler and interpreter.
Before knowing the difference between Compiler and Interpreter, just go through what is compiler and interpreter.
What is Compiler?
The compiler is a translator that simply convert the program from one language to another language, from high-level language to machine level language. This conversion happens in the single shot and gives executable file. The process of compiling a code is called as a compilation. Execution is not carried out while compilation phase. You need to
Execution does not carry out while compilation phase. You need to run executable object separately.
What is Interpreter?
The interpreter is radically different from the compiler. It reads the high-level program code line by line and translate it into machine level code and at the same time it execute each line. (It does not wait for the complete program to translate.)
Note: Compiler and interpreter are itself software program, that converts the program from one language to other.
Now lets, keep it simple with below example.
Example: How does actually compiler and interpreter work?
We keep this example generic. The program is nothing but the set of instructions.
Assume following are the instruction in the program that takes any integer value from the user and manipulates some mathematical operations. After all operations, it prints output.
- Take the user input as integer number (says n)
- Add 10 to number ‘n’
- Multiply it by 5
- Subtract 100
- Divide by 3
- Display the result
How does program run on Compiler?
In the above sample program, a very first instruction is to accept input from the user. Before getting input from a user, it compiles all the program code and saved formula something like as ((n+10)5 -100)/3. It generates the executable object file (machine level language program). This compilation process happens only once.
Now the job of the compiler is completed. Next job is to execute compiled target file.
The user needs to run executable object explicitly. When the user runs it, it ask for user input and save in variable ‘n’. The value of ‘n’ gets substituted in the formulae. The result of the formula will be displayed as output to the user.
We can repeatedly run this program with multiple user inputs without any more compilation.
How does program run on Interpreter?
In a case of an interpreter, it goes through each instruction. For each instruction, it generate machine level code and execute. In above example, it translate first instruction and asks a user for input. It accepts the input and saved in variable ‘n’. Controller move to next instruction and perform addition operation (n+10). Likewise, it follows all the instructions one by one and executes. The result from the last instruction will be displayed as output to the user.
I always love to keep it simple. If you are willing to learn, I have shared article, how does computer network work in simple word.
Difference between compiler and interpreter:
There is a difference between compiler and interpreter, the way it executes the program.
|It scans complete program in one shot and translate it into machine level language.||It reads each statement at once and translate it into another language.|
|It generates the intermediate code, which is used for code optimization.||Intermediate code is not generated by the interpreter.|
|All the errors are spotted at compilation time. So it is less prone to error at execution time.||As it executes code one by one statement, the run-time errors can occur.|
|Errors are generated after analyzing the complete program. So it is hard to debug the compiled language code.||As it scans each statement, it throws an error if it occurs at any statement. So it is easy to debug interpreted language code.|
|As compilation happens only once, it runs faster while execution.||We need to interpret every time to run the program. Its execution is slower than the compiler.|
|The compiler is mostly used by the program that needs high performance.||Language program that needs more flexibility and programmability uses Interpreter. Most of the language that uses interpreter are dynamic and scripting languages.|
|Programming languages that use Compiler: Fortran, Pascal, C/C++, Ada||Programming languages that use Interpreter: Perl, Python, CL, Lisp, Scheme, Bash, AWK|
Is Java compiled or interpreted computer language?
This is the basic question, asks in many Java interviews.
While running java program, it gets converted into byte code and then it runs. Conversion of java to byte-code is carried out by compiler called as javac compiler. Target bytecodes are interpreted using interpreter called as JVM (Java Virtual Machine). So Java is both interpreted and compiled language.
You may also like to read some more differences:
- Difference between Path and Classpath Environment Variable in Java
- Difference between Microprocessor and Microcontroller
In this post, I have tried my best to explain the difference between compiler and interpreter. Compiler and interpreter have its own advantages as well as disadvantages. It is very difficult to decide superiority of compiler over interpreter and vice-versa. If you have any question, feel free to comment bellow.