Compilers vs Interpreters – An overview of the differences - LEKULE

Breaking

17 Dec 2015

Compilers vs Interpreters – An overview of the differences

It is a general notion that people try to classify programming languages as either “compiled” language or “interpreted” language. Even experienced programmers tend to get confused here! But the fact is, programming languages are neither “compiled” nor “interpreted” types. They can be both at the same time. Compiling or interpreting -both are 2 different ways of implementing the same program written using a programming language. A program written in C language can either be compiled or can be interpreted. Same is the case with Java or any other programming languages. The only requirement is, we need a C or Java compiler to compile a C/Java program and similarly we need an C/Java Interpreter to interpret a program written in C/Java.  So the difference is not with programming languages, it is with the way programs written in different languages are implemented.
Any one serious about programming should understand the working of compilers,interpreters and the differences between them.So here I am trying to outline generic differences between compiling and interpreting (compilers vs interpreters).

Though I said programs written in any programming language can be either compiled or interpreted, it is not the case always. Theoretically what I wrote above is right – any program can be compiled/interpreted. But a programming language is usually developed with an orientation to one particular form of execution – for example- C language was designed to be compiled where as Java was designed to be interpreted. But there are interpreters available for C programs too which will be helpful as debugging aids. But in most cases a C program is compiled for execution and not interpreted. Before going through differences, keep in mind  the following  technical terms.

Compile time – The time taken to compile a program.

Run time – The time taken for executing a program.

Source code– The program in its user written form of the language. Source code is given as input to the compiler.

Object code–  is actually the machine code which is obtained by converting source code. The computer can read and execute machine code directly. An object code is also known as binary code/machine code.
working of a compiler
So the primary difference between a compiler and interpreter is in the way a program is executed. A compiler reduces the source code to machine code and then save it as an object code before creating an executable file for the same. When executed, the compiled program is executed directly using the machine code (object code). Where as an interpreter does not convert the source code to an object code before execution. An interpreter executes the source code line by line and conversion to native code is performed line by line while execution is going on (at runtime). In such a scenario, the run time required for an interpreted program will be high compared to a compiled program. Even though the run time required for interpreted program are high, the execution using an interpreter has its own advantages. For example -interpreted programs can modify themselves at runtime by adding/changing functions. A compiled program has to be recompiled fully even for the small modifications we make to a small section of the program; where as an in the case of an interpreter there is no such problem (only the modified section needs to be recompiled).
Difference between and interpreter and compiler
Let us summarize the advantages of both implementation methods – compiling and interpreting

Advantages of using compiler:-
  • Since compiler converts the program to native code of the target machine (object code), faster performance can be expected.
  • There is a scope for code optimisation.  
Advantages of using interpreter:-

  • Process of execution can be done in a single stage. There is no need of a compilation stage.
  • Alteration of codes possible during runtime.
  • Really useful for debugging the codes (because source code execution can be analyzed in an IDE)
  • Facilitates interactive code development.

No comments: