compiled_vs_interpreted.html
1 <!DOCTYPE html> 2 <html lang="de"> 3 <head> 4 <meta charset="UTF-8" /> 5 <meta name="viewport" content="width=device-width, initial-scale=1.0" /> 6 <meta http-equiv="onion-location" content="http://bopbopl6lohkl2rts3ltesjnag4hzs4jrx2h6k6etgq5xasbpqekzlqd.onion" /> 7 <title>BOP Wiki: Compiled vs. Interpreted</title> 8 <link rel="stylesheet" href="/assets/stylesheet.css" /> 9 <link rel="icon" type="image/x-icon" href="/assets/img/favicon.png"> 10 </head> 11 <body> 12 <header> 13 <!-- --------------------------------------------------------------------------------------------------------------------------------- --> 14 <script src="/assets/js/navbar-OpenClose.js"></script> 15 <script src="/assets/js/lightbox.js"></script> 16 <script src="/assets/js/copyCodeButton.js"></script> 17 <link rel="stylesheet" href="/resources/js-libraries/highlightJS/atom-one-dark.min.css"> 18 <script src="/resources/js-libraries/highlightJS/highlight.min.js"></script> 19 <script src="/resources/js-libraries/highlightJS/highlightjs-line-numbers.min.js"></script> 20 <script>hljs.highlightAll();</script> 21 <script>hljs.initLineNumbersOnLoad();</script> 22 <!-- --------------------------------------------------------------------------------------------------------------------------------- --> 23 <div class="branding"> 24 <button class="toggle-btn-navbar" id="navbarOpenButton">☰</button> 25 <a href="/"> 26 <img class="logo" src="/assets/img/logo.png"> 27 </a> 28 <div class="typing-animation">BytesOfProgress</div> 29 </div> 30 </header> 31 <div id="navbarContainer" class="navbar-container"> 32 <iframe class="navbar-iframe" src="/assets/navbar/navbar.html" frameBorder= "0"></iframe> 33 </div> 34 <main> 35 <article class="blog-post"> 36 <header class="post-header"> 37 <h1 class="post-title">Compiled vs. Interpreted</h1> 38 </header> 39 </article> 40 <nav class="breadcrumb"> 41 <a href="/">Home</a> 42 <span class="divider">›</span> 43 <a href="/wiki/">Wiki</a> 44 <span class="divider">›</span> 45 <a href="/wiki/coding/coding.html">Programming / Coding</a> 46 <span class="divider">›</span> 47 <span class="current">Compiled vs. Interpreted</span> 48 </nav> 49 <section class="post-content"> 50 51 52 <p> 53 When you do programming, your computer does not directly understand your program. 54 For the computer to understand it, your program needs to be translated into machine language first. 55 This translation is handled by either a compiler or and interpreter. 56 </p> 57 58 <p> 59 This difference determines how fast programs run, how easy they are to debug, 60 and which languages are better for certain tasks than others. 61 After this article you will know: 62 </p> 63 64 <ul> 65 <li><strong>What compilation and interpretation actually mean</strong> </li> 66 <li><strong>Basic knowledge about how compilers and interpreters operate</strong> </li> 67 <li><strong>Important pros and cons of them</strong> </li> 68 <li><strong>Which popular languages use which method</strong> </li> 69 </ul> 70 71 72 <h2>What does compilation and interpretation actually mean?</h2> 73 74 <ul> 75 <li><strong>Compiler: </strong>A compiler takes the entire source code and 76 converts it into machine code before the program runs. 77 This process checks for errors and optimizes the code for 78 better performance. Once compiled, the program can be executed directly by 79 the processor. making it very fast. The program only needs to be compiled once: 80 The binary that is output by the compiler can be run over and over again.</li> 81 82 <table> 83 <thead> 84 <tr> 85 <th>Pro</th> 86 <th>Contra</th> 87 88 </tr> 89 </thead> 90 <tbody> 91 <tr> 92 <td>Program is executed faster.</td> 93 <td>Debugging can be more complicated.</td> 94 </tr> 95 <tr> 96 <td>Programs can be executed without installing additional components / dependencies.</td> 97 <td>Specifically needs to be compiled for every platform.</td> 98 </tr> 99 <tr> 100 <td>Ability to distribute compiled executables without exposing the source code.</td> 101 <td>Big programs take long time to compile.</td> 102 </tr> 103 <tr> 104 <td>Less overhead when executed because the translation is already done.</td> 105 <td>Changes made to the program require recompilation.</td> 106 </tr> 107 </tbody> 108 </table> 109 110 <li><strong>Interpreter: </strong>An interpreter reads and executes code line 111 by line, translating each instruction into machine code just before running it. 112 If the interpreter notices an error, it stops immediately, which makes debugging easier. 113 But because the program is interpreted every time it is executed, it takes longer to start.</li> 114 </ul> 115 116 <table> 117 <thead> 118 <tr> 119 <th>Pro</th> 120 <th>Contra</th> 121 122 </tr> 123 </thead> 124 <tbody> 125 <tr> 126 <td>Easy to debug because errors are noticed immediately.</td> 127 <td>Slower execution.</td> 128 </tr> 129 <tr> 130 <td>One step less: Program can be executed instantly after writing it.</td> 131 <td>Users might need to install additional dependencies.</td> 132 </tr> 133 <tr> 134 <td>One step less: Program can be executed instantly after writing it.</td> 135 <td>Requires an interpreter on the target system.</td> 136 </tr> 137 <tr> 138 <td>Platform-independent: Every system with an interpreter can run the program.</td> 139 <td>Because the source code is distributed, it is easier to reverse-engineer or copy.</td> 140 </tr> 141 </tbody> 142 </table> 143 144 <!-- --------------------------------------------------------------------------------------------------------------------------------- --> 145 146 147 <h2>How do compilers and interpreters operate?</h2> 148 149 <h3>Compiler: </h3> 150 151 <p> 152 A compiler reads your source code, checks it for 153 correctness (both syntactically and semantically), optimizes it, 154 and foutputs a standalone executable. 155 </p> 156 157 <ul> 158 <li><strong>Lexical Analysis: </strong>This is the first step. The compiler breaks the source code up into tokens / chunks like keywords (int, return), 159 identifiers (variable names), operators (+, *), etc. Basically scanning the code to recognize each component.</li> 160 161 <li><strong>Syntax Analysis / Parsing: </strong>For the second step, the compiler checks the structure of the code and builds a syntax tree (or parse tree) based on grammar rules of the language. 162 For example, it verifies that loops, functions, and conditionals are formed properly. E.g. if there is a semicolon missing or an if/else statement 163 is not done right, the compiler will throw an error.</li> 164 165 <li><strong>Semantic Analysis: </strong>The compiler now checks whether the code actually makes sense. It verifies things like: 166 <ul> 167 <li><strong>Is the program using variables before declaring them?</strong> </li> 168 <li><strong>Are function calls using the correct arguments?</strong> </li> 169 <li><strong>etc.</strong></li> 170 </ul> 171 </li> 172 173 <li><strong>Intermediate Code Generation and Optimization: </strong>The compiler then converts the syntax tree into an intermediate form, 174 which is kind of a halfway language between the source code and machine code. During this step, it also optimizes your program, 175 improving efficiency without changing what it does by simplifying expressions, inline functions, or removing unused variables.</li> 176 177 <li><strong>Code Generation: </strong>Now the compiler produces actual machine code, suited for the specific CPU and operating system. 178 This is where your ".c" file turns into a ".o" or ".obj" file (an object file), which contains low-level instructions, almost ready to be executed.</li> 179 180 <li><strong>Linking: </strong>This is the final step. Most programs are not isolated. They rely on libraries (like the C standard library) or 181 are split into multiple source files. The linker is the tool that takes all these pieces, the object files, any libraries the program is using, 182 and some startup code the OS needs. It puts them together into one final, executable file (like a.out on Linux or program.exe on Windows).</li> 183 </ul> 184 185 <!-- --------------------------------------------------------------------------------------------------------------------------------- --> 186 187 <h3>Interpreter: </h3> 188 189 <p> 190 An interpreter reads your source code and executes it line by line. 191 It performs many of the same checks as a compiler but instead of giving the user a binary file, it runs the source directly. 192 </p> 193 194 <ul> 195 <li><strong>Lexical Analysis: </strong>This is the first step. The interpreter breaks the source code into tokens / keywords, variable names, literals, operators, etc. 196 Just like a compiler, scans the code to identify its components.</li> 197 198 <li><strong>Syntax Analysis / Parsing: </strong>Next, the interpreter parses the tokens into a syntax tree or an abstract syntax tree (AST), 199 ensuring the code follows the syntactical rules of the language. IE.g. if you forget a semicolon or have a wrong loop, the interpreter will throw a syntax error.</li> 200 201 <li><strong>Semantic Analysis: </strong>The interpreter then checks if the code makes logical sense. It verifies: 202 <ul> 203 <li><strong>Are all variables declared before use?</strong></li> 204 <li><strong>Are functions called with the right number/type of arguments?</strong></li> 205 <li><strong>Does the code follow type rules (e.g., adding numbers to strings)?</strong></li> 206 </ul> 207 </li> 208 209 <li><strong>Interpretation / Execution: </strong>This is where the main difference to a compiler is. Instead of converting the whole program into machine code, 210 the interpreter directly executes the syntax tree or another intermediate form, instruction by instruction. 211 This means it translates and runs the code on the fly, without creating a separate executable.</li> 212 213 <li><strong>Runtime Handling: </strong>Since the code is executed dynamically, runtime errors (e.g., division by zero, accessing undefined variables) 214 are detected and thrown while the code runs. This is why interpreted languages often feel more flexible but may have slower performance.</li> 215 </ul> 216 217 <!-- --------------------------------------------------------------------------------------------------------------------------------- --> 218 219 <h3>JIT: Just-in-Time compilation:</h3> 220 221 <p> 222 Just-In-Time (JIT) compilation is a hybrid approach that combines features of interpreters and compilers. 223 Instead of compiling the whole program before or interpreting it line by line during execution, 224 a JIT compiler begins by interpreting the code or executing it in an intermediate form, such as bytecode. While the program runs, 225 the system identifies frequently used sections—called "hot spots". 226 Then it compiles only those hot spots into optimized machine code while the program is still running. 227 A popular example for this is Java's JVM (Java Virtual Machine). 228 </p> 229 230 <!-- --------------------------------------------------------------------------------------------------------------------------------- --> 231 232 <h2>Which popular programming languages use which method?</h2> 233 234 <table> 235 <thead> 236 <tr> 237 <th>Programming Language</th> 238 <th>Type</th> 239 </tr> 240 </thead> 241 <tbody> 242 <tr><td>C</td><td>Compiled</td></tr> 243 <tr><td>C++</td><td>Compiled</td></tr> 244 <tr><td>Python</td><td>Interpreted</td></tr> 245 <tr><td>Java</td><td>JIT</td></tr> 246 <tr><td>JavaScript</td><td>Interpreted</td></tr> 247 <tr><td>Go</td><td>Compiled</td></tr> 248 <tr><td>Assembly</td><td>Compiled</td></tr> 249 <tr><td>PHP</td><td>Interpreted</td></tr> 250 <tr><td>Swift</td><td>Compiled</td></tr> 251 <tr><td>Ruby</td><td>Interpreted</td></tr> 252 <tr><td>Rust</td><td>Compiled</td></tr> 253 <tr><td>MATLAB</td><td>Interpreted</td></tr> 254 <tr><td>Scratch</td><td>Interpreted</td></tr> 255 <tr><td>R</td><td>Interpreted</td></tr> 256 <tr><td>Perl</td><td>Interpreted</td></tr> 257 <tr><td>Objective-C</td><td>Compiled</td></tr> 258 <tr><td>Bash</td><td>Interpreted</td></tr> 259 <tr><td>Lua</td><td>Interpreted</td></tr> 260 <tr><td>C#</td><td>JIT</td></tr> 261 </tbody> 262 </table> 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 <!-- --------------------------------------------------------------------------------------------------------------------------------- --> 286 287 </section> 288 <footer class="post-footer"> 289 <a href="/wiki/coding/coding.html" class="cta-button">← Back</a> 290 </footer> 291 </main> 292 </body> 293 </html>