4.4 Comparison of Interpreters and Compilers

Table 4.1 summarizes the advantages and disadvantages of compilation and pure interpretation. The primary difference between the two approaches is speed of execution. Interpreting high-level language is slower than interpreting object code primarily because decoding high-level statements and expressions is slower than decoding machine instructions. Moreover, a statement must be decoded as many times as it is executed in a program, even though it may appear in the program only once and the result of that decoding is the same each time. For instance, consider the following loop in a C fragment, which computes 21,000,000 iteratively:6

Table 4.1 Advantages and Disadvantages of Compilers and Interpreters

Implementation

Advantages

Disadvantages

Traditional Compiler

fast execution;

compile once, run repeatedly

inconvenient program development; no REPL;

less source-level debugging;

less run-time flexibility

Pure Interpreter

convenient program development; REPL;

direct source-level debugging;

run-time flexibility

slow execution (decoding);

often requiresmore run-time space

A set of three code lines in C.
Description

If this program was purely interpreted, the statement Statement reads: result asterisk equals 2. would be decoded once less than 1 million times! Thus, not only does a software interpreter decode a high-level statement such as Statement reads: result asterisk equals 2. more slowly than the processor decodes the analogous machine instruction, but that performance degradation is compounded by repeatedly decoding the same statement every time it is executed. An interpreter also typically requires more run-time space because the run-time environment—a data structure that provides the bindings of variables—is required during interpretation (Chapter 6). Moreover, often the source program is represented internally with a data structure designed for convenient access, interpretation, and modification rather than one with minimal space requirements (Chapter 9). Often the internal representation of the source program accessed and manipulated by an interpreter is an abstract-syntax tree. An abstract-syntax tree, like a parse tree, depicts the structure of a program. However, unlike a parse tree, it does not contain non-terminals. It also structures the program in a way that facilitates interpretation (Chapters 1012).

The advantages of a pure interpreter and the disadvantages of a traditional compiler are complements of each other. At a core level, program development using a compiled language is inconvenient because every time the program is modified, it must be recompiled to be tested and often the programmer cycles through a program-compile-debug-recompile loop ad nauseam. Program development with an interpreter, by comparison, involves one less step. Moreover, if provided with an interpreter, a read-eval-print loop (REPL) facilitates testing and debugging program units (e.g., functions) in isolation of the rest of the program, where possible.

Since an interpreter does not translate a program into another representation (other than an abstract-syntax representation), it does not obfuscate the original source program. Therefore, an interpreter can more accurately identify source-level (i.e., syntactic) origins (e.g., the name of an array whose index is out-of-bounds) of run-time errors and refer directly to lines of code in error messages with more precision than is possible in a compiled language. A compiler, due to translation, may not be able to accurately identify the origin of a compile-time error in the original source program by the time the error is detected. Run-time errors in compiled programs are similarly difficult to trace back to the source program because the target program has no knowledge of the original source program. Such run-time feedback can be invaluable to debugging a program. Therefore, the mechanics of testing and debugging are streamlined and cleaner using an interpreted, as opposed to a compiled, language.

Also, consider that a compiler involves three languages: the source and target languages, and the language in which the compiler is written. By contrast, an interpreter involves only two languages: the source language and the language in which the interpreter is written—sometimes called the defining programming language or the host language.

6. This code will not actually compute 21,000,000 because attempting to do so will overflow the integer variable. This code is purely for purposes of discussion.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.22.181.47