Saturday, 4 October 2014

Compiler vs Interpreter: Difference Between Compiler and Interpreter

Difference Between Compiler and Interpreter





A source code is the single form of program readable by human beings and understandable by computers. Generally, a computer program code is written in some high level programming languages such as CC++FortranJava, or any other which contains words and phrases from the English (or any other) language. But, a computer does not understand high level language as it only understands program written in binary form (in 0’s and 1’s), called the machine code. Special softwares in the form of compilers and interpreters are used to translate the source code written in high level language into the executable and error-free machine code format. Though the major function of both of these is similar, there are numerous difference between compiler and interpreter.
Difference Between Compiler and Interpreter

A compiler and a interpreter both carry out the same purpose – convert high level language code into the binary form understandable by the computer hardware. These are basically softwares used to execute the high level instructions to perform tasks specified in the program. Compilers and interpreter are specifically designed for different high level language platforms. Although compiler and interpreter both have the same primary objective, they differ in the way they accomplish it. So, they primarily differ in the way they convert high level language code into machine code. Through this post, we will discuss the basic working principles of both, and list out the basic difference between compiler and interpreter.

Compiler:

Difference between compiler and interpreter
Process of Compilation
A compiler is a special type of program that translates a source code into its object code. High level programming languages come with a compiler of their own which defines the acceptable instructions. So, when a program code is written in C language, a specific compiler designed for C is used for compilation before the source code is executed. The compiler first scans the entire source code and translates it into machine code before it is executed by the computer processor, and then the tasks specified in the program are performed.
Difference between compiler and interpreter - Compilation vs interpretation
Compilation
The first step in compilation is to pass the source code into the compiler so that it is translated into theobject code. The compiler then sends object file (object code) into the linker from where the program comes out as an executable one. Intermediate code is not formed in compilation process.

Interpreter:

Difference between compiler and interpreter - Compilation vs interpretation
Interpretation
Like a compiler, an interpreter is also a special kind of program for the conversion of a high-level program statement into machine code just before the program statement is to be executed. During each conversion, the interpreter converts the source code into an intermediate code before processing it into the machine for the generation of machine code. Each part of the code is interpreted line by line and then executed separately in a sequential manner. So, if an error is found in any part of the code, the interpreter will stop the interpretation without converting the next set of codes.
As interpreters check the source code line by line, translation and execution occurs immediately one statement at a time. As a result, they are 2 to 10 times slower than compilers. But this very disadvantage makes interpreters easier to user, especially for beginners; errors once found are immediately displayed and corrected by the user, before the program is executed. Listed below are some of the key features of interpreter:
  • The input to the interpreter is a single instruction.
  • Interpreter converts the source code into an intermediate code before processing it into the machine.
  • During interpretation, the condition control statements execute slowly.
  • Memory requirement is less.
  • Errors found are displayed instantly.
  • Since an interpreter is more interactive, it is used in teaching-learning sectors.

Difference Between Compiler and Interpreter:


  1. As already aforementioned, the major difference between compiler and interpreter is how they accomplish their primary objective. A compiler converts all the statements of source code into theobject code and then finally into the exe file. On the other hand, an interpreter converts each statement of the source code into machine before execution of the statement. Interpreter does not produce any exe file.
  2. Once complied, a program does not need to be compiled again and again for the next run, whereas the interpreted program requires to be interpreted for each run of the program.
  3. Compiler takes the entire program as input, whereas interpreter takes only a single instruction as input.
  4. The memory requirement in case of compiler is more than that compared to interpreter.
  5. Another important difference between compiler and interpreter is that – in case of compiler, errors are displayed after the entire program is checked. But, in the case of interpreters, errors, if found, are displayed for every instruction interpreted.
  6. Compiler checks the source code at once, but interpreter checks the code line by line. In this respect, interpreters are considered to be 2 to 10 times slower than compilers.
  7. Before producing an executable file, compilers take some time, but interpreters can execute the code immediately. Taking this into consideration, compilers can be said to be slower than interpreters.
  8. Conditional control statements execute faster with compiler than with interpreter.
  9. Another remarkable difference between compiler and interpreter is that in compilation, the immediate editing and execution is considered to be a costly process because it takes longer time for the compilation of a program if the program is lengthy. On the other hand, with interpreters, it is possible to execute the edited statement immediately after its editing. So, interpreter is more interactive, easier to use, and is considered better for students.
  10. Compiler example: C compiler, C++ compiler, Fortran compiler, etc.
    Interpreter example: LISPBASIC, etc.
So, to sum it up, compilers and interpreters carry out the same objective, but they do it differently. Programming languages like C, C++ use compilers whereas others such as Python and Ruby use interpreters. I hope this article helped you understand compilation and interpretation process as well in addition to the 10 basic difference between compiler and interpreter

No comments:

Post a Comment