algorithmicCoder
algorithmicCoder

Reputation: 6799

Why is an interpreter slower than a compiler in practice?

Don't they both have to convert to machine code at some point to execute or am i missing something more basic?

EDIT:

Please consider more sophisticated interpreting schemes e.g. cases where the code is translated to Byte code and the byte code is only regenerated when source code changes e.g. CPython implementation of Python? I am not really interested in ancient interpreters that re-execute line by line....

Thanks!

Upvotes: 20

Views: 18269

Answers (7)

Mithun Sasidharan
Mithun Sasidharan

Reputation: 20950

As per WikiPedia:

Interpreting code is slower than running the compiled code because the interpreter must analyze each statement in the program each time it is executed and then perform the desired action, whereas the compiled code just performs the action within a fixed context determined by the compilation. This run-time analysis is known as "interpretive overhead". Access to variables is also slower in an interpreter because the mapping of identifiers to storage locations must be done repeatedly at run-time rather than at compile time.

Upvotes: 2

Gustav Bertram
Gustav Bertram

Reputation: 14905

A compiled language like C is usually compiled directly into machine code. When you run the code, it is executed directly by the CPU.

A fully interpreted language like BASIC or PHP is usually interpreted each time it runs. When you execute your code, the CPU executes the interpreter, and the interpreter reads and executes your source code. (PHP can be classified as fully interpreted, since while it does use opcodes, they are usually thrown away after the execution.)

A bytecode interpreted language like Python, is compiled from source code to bytecode that is executed by a virtual machine. The CPU runs the VM, and the VM executes each bytecode instruction. In Python, the bytecode is compiled the first time code is executed.

In Java, bytecode is compiled ahead of execution. The Java VM also has a special feature called Just-in-time compilation. This means that during execution, it may compile some of the bytecode to machine code, which it can send to the CPU to execute directly.

In conclusion, with compiled languages, the CPU runs the code directly. In interpreted languages, the CPU usually runs the interpreter or virtual machine. This makes interpreted languages generally slower than compiled languages, due to the overhead of running the VM or interpreter.

NOTE: While we speak of interpreted and compiled languages, what we are really discussing is the usual execution style of a language. PHP can be compiled (using HipHop), and C can be interpreted (using Parrot VM).

Upvotes: 26

SK-logic
SK-logic

Reputation: 9725

A borderline between compilation and interpretation is blurred. In general, some forms of interpretation are working slower than a directly compiled code. It may not necessarily be true in specific cases.

For example, some interpreters are directly executing virtual machine instructions (sometimes translated into a direct or indirect threaded code), which is slower than a native code for obvious reasons.

Since there is an impedance mismatch between semantics of the VM (e.g., most of them are stack-based) and semantics of the native code, any ad hoc translation from one to another will be sub-optimal. In order to perform heavyweight optimisations you'll need a virtual machine (or any other form of an intermediate representation) with a certain amount of a target platform specific semantics in it. LLVM is a good example of such a representation.

Some other interpreters are even evaluating the code on an abstract syntax tree level. Some are performing string substitution (a notorious example is Tcl).

All that techniques are easy to implement, they provide some interesting dynamic properties to the language semantics, but all at a cost of a slower execution.

Another important thing to mention is a supercompilation. This technique practically turns any interpreter into a compiler, by specialising the interpreter implementation against a specific instance of a code to be executed. Existence of such approaches renders a difference between compilation and interpretation even more vague.

Upvotes: 3

chill
chill

Reputation: 16898

OK, a lot of incorrect posts here, time for a long answer.

A compiler is basically clear - it translates a program form the source language to the target language. Both languages can be whatever - high level language, virtual machine bytecode, machine code.

An interpreter, on the other hand does not perform a translation, but directly performs the actions, prescribed by the source language construct, a.k.a. interprets it.

Let's consider a hypothetical add instruction in a stack based machine, which adds the two top elements of the stack and pushes the result back. An interpreter will directly perform that "add the two top elements and push the result back", in a manner similar to:

switch (op)
{

 ....

  case OP_ADD:  
    op1 = pop (stack);
    op2 = pop (stack);
    res = op1 + op2;
    push (stack, res);
    break;

...
}

As you can see, for a single add insn, there are many operations performed: reading and writing memory, incrementing and decrementing the stack pointer, the add operation itself, the overhead of the switch (if the interpreter is implemented that way), the overhead of the loop which reads each subsequent insn and decides how to process it, etc.

If the interpeter worked on an AST, it may look like:

swicth (op)
{
   ...
   case OP_ADD:
     op1 = tree->eval (left);
     op2 = tree->eval (right);
     return op1 + op2;
   ...
}

Again, many, many insns to perform whatever is required by the add semantics.

Upvotes: 7

Upul Bandara
Upul Bandara

Reputation: 5958

Interpreter executes line by line and converts each line to machine instruction at run time. Whereas compiler converted entire program from source language to target language ( most probably machine instruction of the target processor ) in the compile time.

Upvotes: -1

Valmond
Valmond

Reputation: 2969

The compiler does it once (which takes some time), then the code runs fast. As it can take quite a while, it can spend quite some time optimizing the code too.

The interpreter does it when you want to run the code, so it compiles each time you run it.

Upvotes: 0

AndersK
AndersK

Reputation: 36092

A compiler translates a program to machine code before you run the program. It resolves all variables, types at compile time.

An interpreter typically translates each statement to machine code each time the statement is executed.

Upvotes: 3

Related Questions