210 likes | 438 Views
Compiled vs Interpreted. A tradeoff of flexibility vs efficiency. Similarity. The function of a compiler or interpreter is to translate a Higher Level Language (HLL) and execute it. Compiler or Interpreter. Output. HLL. Major Difference. Timing
E N D
Compiled vs Interpreted A tradeoff of flexibility vs efficiency
Similarity • The function of a compiler or interpreter is to translate a Higher Level Language (HLL) and execute it. Compiler or Interpreter Output HLL
Major Difference • Timing • Amount of code translated before executing • Oversimplified example • Translate a book -> Then read the book • Translate a statement -> Read statement (REPEAT) • Statement(word) at a time or do entire translation first.
Environments (extremes) • Compiled • translate into object code • link • execute • g++ is a script which hides steps • g++ -c HelloW.C • creates HelloW.o • g++ -o HelloW HelloW.o • links/creates HelloW
Environments (continued) • Interpreted • Read line, translate, execute • Repeat • unix basic calculator (bc) program America>bc a = 12 + 4 a 16 b = a + 2 b 18 quit America>
Other languages • Lots of languages are interpreted • Basic • Smalltalk • APL • java • Our Tannenbaum machine language
Target code • Compilers • HLL -> Machine language of the executing machine • Interpreters • HLL -> intermediate code • program reads those codes, translates by executing lots of machine language instructions
Example (Tannenbaum) • A compiler would translate the pascal program directly into ML. If there was a real machine, what you see in ML would be the result of compilation. Target is native ML. • A := 4; -> LOCO 4 STOD A • Instead, this is a virtual machine. The resulting ML requires us to execute hundreds of c++ instructions in our simulator and each of those c++ instructions requires multiple real ML instructions on the Sun. E.g. • A;=4 -> 2 ML (tann) -> 30 c++ -> 100 ML (sun)
Binding Times • Compilers • Make decisions as early as possible • Results are more efficiently implemented • Interpreters • make decisions late • Results less efficient • PROGRAMMERS more FLEXIBILITY
XML – delaying decisions <note> <to>cs260</to> <from>Prof. Game</from> <heading>Announcement</heading> <body>Test on Wednesday!</body> </note> versus cs260 Prof. Game Announcement Test on Wednesday!
Binding Time example Interpreter: a:=4; a:=“Hello”; Requires a RUN-TIME DESCRIPTOR. (see next slide). Code must check: if (numeric) … else if (string) … else ... Compiler: a:=4; Decide where to store the variable and simple translation like what we saw in the Tannenbaum example. A few ML instructions.
Descriptor int 45 A B C float 12.456 Hello string 5 length value type Managed during execution. Examined at each reference!
Now do B:=‘xyz’ int 45 A B C xyz string 3 Hello string 5 length value type B had it’s value changed, so descriptor also changed. Not as simple as STOD B.
Grey areas • Most languages are a combination • Pascal/Tannenbaum example • A:=4; -> direct translation in ML • writeln(A); -> must interpret with a procedure. • Java • byte codes for virtual machine • JIT (just in time) compiler to translate parts of it for the purpose of efficiency
How do we use this categorization? • Think of it more in terms of features rather than the entire language. • Tradeoff is Efficiency vs Flexibility • This gives us a sense of whether the feature or language executes to provide optimal performance or perhaps optimizes other factors such as flexibility of target/platform such as in java.