|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "<small><small><i>\n", |
| 8 | + "All the IPython Notebooks in this lecture series by Dr. Milan Parmar are available @ **[GitHub](https://github.com/milaan9/01_Python_Introduction)**\n", |
| 9 | + "</i></small></small>" |
| 10 | + ] |
| 11 | + }, |
| 12 | + { |
| 13 | + "cell_type": "markdown", |
| 14 | + "metadata": {}, |
| 15 | + "source": [ |
| 16 | + "# Interpreter Vs Compiler : Differences Between Interpreter and Compiler\n", |
| 17 | + "\n", |
| 18 | + "In this class, you will learn the differences between interpreters and compilers.\n", |
| 19 | + "\n", |
| 20 | + "We generally write a computer program using a high-level language. A high-level language is one that is understandable by us, humans. This is called **source code**.\n", |
| 21 | + "\n", |
| 22 | + "However, a computer does not understand high-level language. It only understands the program written in **0**'s and **1**'s in binary, called the **machine code**.\n", |
| 23 | + "\n", |
| 24 | + "To convert source code into machine code, we use either a **compiler** or an **interpreter**." |
| 25 | + ] |
| 26 | + }, |
| 27 | + { |
| 28 | + "cell_type": "markdown", |
| 29 | + "metadata": {}, |
| 30 | + "source": [ |
| 31 | + "## What is Interpreter?\n", |
| 32 | + "\n", |
| 33 | + "An interpreter is a computer program, which coverts each high-level program statement into the machine code. This includes source code, pre-compiled code, and scripts. Both compiler and interpreters do the same job which is converting higher level programming language to machine code. However, a compiler will convert the code into machine code (create an exe) before program run. Interpreters convert code into machine code when the program is run.\n", |
| 34 | + "\n", |
| 35 | + "\n", |
| 36 | + "## What is Compiler?\n", |
| 37 | + "\n", |
| 38 | + "A compiler is a computer program that transforms code written in a high-level programming language into the machine code. It is a program which translates the human-readable code to a language a computer processor understands (binary 1 and 0 bits). The computer processes the machine code to perform the corresponding tasks.\n", |
| 39 | + "\n", |
| 40 | + "A compiler should comply with the syntax rule of that programming language in which it is written. However, the compiler is only a program and cannot fix errors found in that program. So, if you make a mistake, you need to make changes in the syntax of your program. Otherwise, it will not compile.\n", |
| 41 | + "\n", |
| 42 | + "\n", |
| 43 | + "## Programming Steps Comparision:\n", |
| 44 | + "\n", |
| 45 | + "| # | Interpreter | Compiler |\n", |
| 46 | + "|:----:|:----|:-------|\n", |
| 47 | + "|**1.**|Create the Program.|Create the program.|\n", |
| 48 | + "|**2.**|No linking of files or machine code generation.|Compile will parse or analyses all of the language statements for its correctness. If incorrect, throws an error.|\n", |
| 49 | + "|**3.**|Source statements executed line by line DURING Execution.|If no error, the compiler will convert source code to machine code.|\n", |
| 50 | + "|**4.**|-|It links different code files into a runnable program(know as exe).|\n", |
| 51 | + "|**5.**|-|Run the Program|" |
| 52 | + ] |
| 53 | + }, |
| 54 | + { |
| 55 | + "cell_type": "markdown", |
| 56 | + "metadata": {}, |
| 57 | + "source": [ |
| 58 | + "Both compilers and interpreters are used to convert a program written in a high-level language into machine code understood by computers. However, there are differences between how an interpreter and a compiler works.\n", |
| 59 | + "\n", |
| 60 | + "<div>\n", |
| 61 | + "<img src=\"img/IvsC.png\" width=\"600\"/>\n", |
| 62 | + "</div>" |
| 63 | + ] |
| 64 | + }, |
| 65 | + { |
| 66 | + "cell_type": "markdown", |
| 67 | + "metadata": {}, |
| 68 | + "source": [ |
| 69 | + "## Interpreter Vs Compiler\n", |
| 70 | + "\n", |
| 71 | + "| Parameters | Interpreter | Compiler |\n", |
| 72 | + "|:-----|:----|:-------|\n", |
| 73 | + "|Advantage|Interpreters are easier to use, especially for beginners.|The program code is already translated into machine code. Thus, it code execution time is less.|\n", |
| 74 | + "|Disadvantage|Interpreted programs can run on computers that have the corresponding interpreter.|You can't change the program without going back to the source code.|\n", |
| 75 | + "|Machine code|Not saving machine code at all.|Store machine language as machine code on the disk|\n", |
| 76 | + "|Running time|Interpreted code run slower|Compiled code run faster|\n", |
| 77 | + "|Model|It is based on Interpretation Method.|It is based on language translation linking-loading model.|\n", |
| 78 | + "|Program generation|Do not generate output program. So they evaluate the source program at every time during execution.|Generates output program (in the form of exe) which can be run independently from the original program.|\n", |
| 79 | + "|Execution|Program Execution is a part of Interpretation process, so it is performed lone statement at a time.|Program execution is separate from the compilation. It scans the entire program and translates it as a whole into machine code.|\n", |
| 80 | + "|Memory requirement|The interpreter exists in the memory during interpretation.|Target program execute independently and do not require the compiler in the memory.|\n", |
| 81 | + "|Best suited for|For web environments, where load times are important. Due to all the exhaustive analysis is done, compiles take relatively larger time to compile even small code that may not be run multiple times. In such cases, interpreters are better.|Bounded to the specific target machine and cannot be ported. C and C++ are a most popular a programming language which uses compilation model.|\n", |
| 82 | + "|Code Optimization|Interpreters see code line by line, and thus optimizations are not as robust as compilers|The compiler sees the entire code upfront. Hence, they perform lots of optimizations that make code run faster|\n", |
| 83 | + "|Dynamic Typing|Interpreted languages support Dynamic Typing|Difficult to implement as compilers cannot predict what happens at turn time.|\n", |
| 84 | + "|Usage|It is best suited for the program and development environment.|It is best suited for the Production Environment|\n", |
| 85 | + "|Error execution|The interpreter reads a single statement and shows the error if any. You must correct the error to interpret next line.|Compiler displays all errors and warning at the compilation time. Therefore, you can't run the program without fixing errors|\n", |
| 86 | + "|Input|It takes a single line of code.|It takes an entire program|\n", |
| 87 | + "|Output|Interpreter never generate any intermediate machine code.|Compliers generates intermediate machine code.|\n", |
| 88 | + "|Errors|Displays all errors of each line one by one.|Display all errors after, compilation, all at the same time.|\n", |
| 89 | + "\n", |
| 90 | + "\n", |
| 91 | + "| Paramters | Interpreters usually take less amount of time to analyze the source code. However, the overall execution time is comparatively slower than compilers. | Compilers usually take a large amount of time to analyze the source code. However, the overall execution time is comparatively faster than interpreters. |\n", |
| 92 | + "| Paramters | It does not convert source code into object code instead it scans it line by line. | It converts the source code into object code. |\n", |
| 93 | + "| Paramters | Considering it scans code one line at a time, errors are shown line by line. | As it scans the code in one go, the errors (if any) are shown at the end together. |\n", |
| 94 | + "| Paramters | No Object Code is generated, hence are memory efficient. | Generates Object Code which further requires linking, hence requires more memory. |\n", |
| 95 | + "| Programming languages | JavaScript, MATLAB, Python, Ruby, PHP, Pearl use interpreters. | C, C++, C#, Java use compilers. |" |
| 96 | + ] |
| 97 | + }, |
| 98 | + { |
| 99 | + "cell_type": "code", |
| 100 | + "execution_count": null, |
| 101 | + "metadata": {}, |
| 102 | + "outputs": [], |
| 103 | + "source": [ |
| 104 | + "https://www.guru99.com/difference-compiler-vs-interpreter.html" |
| 105 | + ] |
| 106 | + }, |
| 107 | + { |
| 108 | + "cell_type": "markdown", |
| 109 | + "metadata": {}, |
| 110 | + "source": [ |
| 111 | + "## KEY DIFFERENCE\n", |
| 112 | + "\n", |
| 113 | + "* Compiler transforms code written in a high-level programming language into the machine code, at once, before program runs, whereas an Interpreter coverts each high-level program statement, one by one, into the machine code, during program run.\n", |
| 114 | + "* Compiled code runs faster while interpreted code runs slower.\n", |
| 115 | + "* Compiler displays all errors after compilation, on the other hand, the Interpreter displays errors of each line one by one.\n", |
| 116 | + "* Compiler is based on translation linking-loading model, whereas Interpreter is based on Interpretation Method.\n", |
| 117 | + "* Compiler takes an entire program whereas the Interpreter takes a single line of code." |
| 118 | + ] |
| 119 | + }, |
| 120 | + { |
| 121 | + "cell_type": "markdown", |
| 122 | + "metadata": {}, |
| 123 | + "source": [ |
| 124 | + "## Similarities between Compiler and Interpreter\n", |
| 125 | + "\n", |
| 126 | + "Yes, there are some similarities between compiler and interpreter. Now we will discuss the similarities here in bullet points. Let’s check out the points to find the similarities.\n", |
| 127 | + "\n", |
| 128 | + "* Both compiler and interpreter translate source code for machine understanding so that your device can follow your command.\n", |
| 129 | + "* The compiler and the interpreter use memory to store data.\n", |
| 130 | + "* Interpreter and Compiler are the high-level languages.\n", |
| 131 | + "* Java language uses both the compiler and the interpreter." |
| 132 | + ] |
| 133 | + }, |
| 134 | + { |
| 135 | + "cell_type": "markdown", |
| 136 | + "metadata": {}, |
| 137 | + "source": [ |
| 138 | + "## Authors ✍️\n", |
| 139 | + "\n", |
| 140 | + "I'm Dr. Milaan Parmar and I have written this tutorial. If you think you can add/correct/edit and enhance this tutorial you are most welcome🙏\n", |
| 141 | + "\n", |
| 142 | + "See **[github's contributors page](https://github.com/milaan9/01_Python_Introduction/graphs/contributors)** for details.\n", |
| 143 | + "\n", |
| 144 | + "If you have trouble with this tutorial please tell me about it by **[Create an issue on GitHub](https://github.com/milaan9/01_Python_Introduction/issues/new)**. and I'll make this tutorial better. This is probably the best choice if you had trouble following the tutorial, and something in it should be explained better. You will be asked to create a GitHub account if you don't already have one.\n", |
| 145 | + "\n", |
| 146 | + "If you like this tutorial, please **[give it a ⭐ star](https://github.com/milaan9/01_Python_Introduction/blob/main/README.md#how-can-i-thank-you-for-writing-and-sharing-this-tutorial)**." |
| 147 | + ] |
| 148 | + }, |
| 149 | + { |
| 150 | + "cell_type": "markdown", |
| 151 | + "metadata": {}, |
| 152 | + "source": [ |
| 153 | + "## Licence 📜\n", |
| 154 | + "\n", |
| 155 | + "You may use this tutorial freely at your own risk. See **[LICENSE](https://github.com/milaan9/01_Python_Introduction/blob/main/LICENSE)**." |
| 156 | + ] |
| 157 | + }, |
| 158 | + { |
| 159 | + "cell_type": "code", |
| 160 | + "execution_count": null, |
| 161 | + "metadata": {}, |
| 162 | + "outputs": [], |
| 163 | + "source": [] |
| 164 | + } |
| 165 | + ], |
| 166 | + "metadata": { |
| 167 | + "hide_input": false, |
| 168 | + "kernelspec": { |
| 169 | + "display_name": "Python 3", |
| 170 | + "language": "python", |
| 171 | + "name": "python3" |
| 172 | + }, |
| 173 | + "language_info": { |
| 174 | + "codemirror_mode": { |
| 175 | + "name": "ipython", |
| 176 | + "version": 3 |
| 177 | + }, |
| 178 | + "file_extension": ".py", |
| 179 | + "mimetype": "text/x-python", |
| 180 | + "name": "python", |
| 181 | + "nbconvert_exporter": "python", |
| 182 | + "pygments_lexer": "ipython3", |
| 183 | + "version": "3.8.8" |
| 184 | + }, |
| 185 | + "toc": { |
| 186 | + "base_numbering": 1, |
| 187 | + "nav_menu": {}, |
| 188 | + "number_sections": true, |
| 189 | + "sideBar": true, |
| 190 | + "skip_h1_title": false, |
| 191 | + "title_cell": "Table of Contents", |
| 192 | + "title_sidebar": "Contents", |
| 193 | + "toc_cell": false, |
| 194 | + "toc_position": {}, |
| 195 | + "toc_section_display": true, |
| 196 | + "toc_window_display": false |
| 197 | + }, |
| 198 | + "varInspector": { |
| 199 | + "cols": { |
| 200 | + "lenName": 16, |
| 201 | + "lenType": 16, |
| 202 | + "lenVar": 40 |
| 203 | + }, |
| 204 | + "kernels_config": { |
| 205 | + "python": { |
| 206 | + "delete_cmd_postfix": "", |
| 207 | + "delete_cmd_prefix": "del ", |
| 208 | + "library": "var_list.py", |
| 209 | + "varRefreshCmd": "print(var_dic_list())" |
| 210 | + }, |
| 211 | + "r": { |
| 212 | + "delete_cmd_postfix": ") ", |
| 213 | + "delete_cmd_prefix": "rm(", |
| 214 | + "library": "var_list.r", |
| 215 | + "varRefreshCmd": "cat(var_dic_list()) " |
| 216 | + } |
| 217 | + }, |
| 218 | + "types_to_exclude": [ |
| 219 | + "module", |
| 220 | + "function", |
| 221 | + "builtin_function_or_method", |
| 222 | + "instance", |
| 223 | + "_Feature" |
| 224 | + ], |
| 225 | + "window_display": false |
| 226 | + } |
| 227 | + }, |
| 228 | + "nbformat": 4, |
| 229 | + "nbformat_minor": 2 |
| 230 | +} |
0 commit comments