Why compiler and interpreter are used?
Compiler and interpreter are both source code translators. Source code is what you write in your IDE. Actually, the code you initially write in your IDE is called high-level language (The language which is understandable to humans but not to computers) and that needs to be translated into low-level language (vise versa to high-level language) which is 0s and 1s. And this task is usually done by two agents (system software) i) Compiler, and ii) Interpreter. So, Today we will see how these agents do their tasks differently.
What is Compiler and how it works?
Compiler is a computer program developed to translate high-level language (source code) to low-level language (machine code). Compiler reads the whole source code at once and analyzes the error if there is any. After that, it generates an executable code (machine code) which you need to execute (run) to get your system to perform the task you instructed or to get the output. This executable file, then can be shared with anyone and he/she can execute in his/her system without your actual source code or installing anything, but this file can be system dependent, meaning it may or may not run on your client system.
Advantages and Disadvantages of Compiler
|Advantages of Compiler||Disadvantages of Compiler|
|Run quickly, since they have already been translated||Takes extra memory as it generates separate executable files|
|Can be optimized for CPU, which makes it faster||The executable file is not system independent means it may or may not works in some computers|
|Can be shared without source code||To execute some task it go through extra step of generating executable files|
|Harder to debug code|
What is Interpreter and how it works?
Similar to Compiler, Interpreter also translates source code from high language to low language. But it does it differently, unlike compiler, interpreter reads the source code line by line and warns you at the same time if there’s an error, which makes it easy to debug but slower compared to the compiler. Also, interpreter doesn’t convert your source code into a separate executable file, instead, it does it while executing your program at the same time.
So if you want to share your program(system instruction) with others, you need to share your source code (which invades privacy if you are concerned but makes it easy to change/debug for the client if he/she needs to) and also the client must have an interpreter to execute the file.
Also Read -> Difference between libraries and Framework
Advantages and Disadvantages of Interpreter
|Advantages of Interpreter||Disadvantages of Compiler|
|Can be shared with other irrespective of system configuration, means it’s hardware independent||Slower in comparative of Compiler|
|Easier to Debug||Need to share source code, less secured if you are concerned|
|To execute the file, client need to have interpreter|
Compiler vs Interpreter: Differences between Compiler and Interpreter
I highly recommend you to watch this short video, to understand the difference between interpreter and compiler. It explains it very well with the great animation and analogy.
|Translate the whole source code at once||Translate the source code line by line|
|Compilers takes more time to analyze source code but it’s overall speed is faster than interpreter||Interpreter takes less time in comparative to compiler to analyze source code, but overall speed is slower than compiler|
|Takes extra memory as it generates a separate executable file||Takes less memory in comparative to compiler|
|Compiler shows all error at once, making it harder to debug||As the interpreter runs the code line by line, it displays the error when it gets it. Which makes it easier to debug|
|Clients can execute the codeWithout source code (with executable file)Without downloading any compiler or anythingOnly if the hardware configuration matches means executable file is not independent to system||Clients can execute the codeOnly when he/she have source codeIf he/she have interpreter installedIrrespective of system configuration (system independent)|
Note: Java, Python, C#, VB.NET are Hybrid means they use both.
If you want to learn more about Compiler and Interpreter than you can visit here
Data Scientist with 3+ years of experience in building data-intensive applications in diverse industries. Proficient in predictive modeling, computer vision, natural language processing, data visualization etc. Aside from being a data scientist, I am also a blogger and photographer.