Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

Looking at most (if not all) dynamic languages [i.e Python, PHP, Perl and Ruby], they are all interpreted. Correct me if I'm wrong. Is there any example of dynamic language that goes through compilation phase? Is dynamic language identical with interpreted language?

share|improve this question
3  
Define dynamic language, is it dynamically typed? –  Ubiquité Jul 1 '11 at 1:10
2  
Objective-C exhibits many "dynamic" properties. –  Crazy Eddie Jul 1 '11 at 2:29
1  
You can already generate and compile .Net code into IL at run-time. .Net 5 hints at creating a "compiler as a service". What is a difference between compiled and interpreted language at that point? –  Job Jul 1 '11 at 3:27
3  
@Job, one could do it with Lisp for decades. And it is both compiled and dynamically typed. So, there had never been an exact borderline between compilation and interpretation. –  SK-logic Jul 1 '11 at 6:28
2  
@Darien You can compile that at run time as well and execute code afterwards. Strictly speaking, it's not interpretation. –  LeakyCode Jul 1 '11 at 6:46

10 Answers 10

up vote 20 down vote accepted

Looking at most (if not all) dynamic languages [i.e Python, PHP, Perl and Ruby], they are all interpreted.

Not true. You can compile Python source. That's one existential proof.

There are interpreters for statically-typed languages, and compilers for dynamically-typed languages. The two concepts are orthogonal.

Side note: In general, a language is just that: a language, with a set of syntactic constructs to express semantics. If you write Python on a whiteboard, it's still called Python! It's the implementation that can be an interpreter or a compiler. Being statically-typed or dynamically-typed (of kind of a hybrid of both) is a property of the language, while executing a program by interpreting or compilation is a property of the implementation.

share|improve this answer
13  
To what precision must indents match on a whiteboard for the Python to be syntactically valid? ;) –  edA-qa mort-ora-y Jul 1 '11 at 7:55
    
You can not compile Python. PYC accelerate only the load of a module. And py2exe simply embed the interpretor into the exe with the source file. –  Ubiquité Jul 1 '11 at 13:11
4  
@Ubiquité: .pyc files are bytecode. Python source code was parsed, optimized and compiled to create them. The bytecode instructions are relatively high-level and the most popular implementation of it is a plain interpreter (for contrast, look at PyPy which JIT-compiles bytecode to very clever machine code at runtime) but Python isn't any less compiled than Java or C#. Python is only "not compiled" if "compilation" was restricted to native ahead-of-time compilation, but nobody said anything about that and generally it can refer to any lanugage-to-language transformation. –  delnan Jul 1 '11 at 13:26
    
from the documentation : A program doesn’t run any faster when it is read from a .pyc or .pyo file than when it is read from a .py file; the only thing that’s faster about .pyc or .pyo files is the speed with which they are loaded. –  Ubiquité Jul 1 '11 at 14:31
2  
@Ubiquité: Yes, that is correct, but that stands in no relation with your claim that "You can not compile Python" or whether it is possible to compile Python. First and foremost, you are mixing Pythonand CPython, whereas the latter is an implementation of the former, so is PyPy. –  phant0m Jul 1 '11 at 16:23

Common Lisp is dynamically (and strongly) typed and usually compiled.

Since this dynamic-ness is achieved at runtime, there are some directives you can use in source code to assure the compiler that a symbol will hold only a certain kind of value, so that the compiler can optimize the generated code and boost performance.

share|improve this answer

C# 4.0 supports dynamic types (late-binding) and it is compiled.

share|improve this answer

node.js is based on Google's V8 javascript engine. V8 does runtime compilation. V8 is blindingly fast given that fact. Just check out http://shootout.alioth.debian.org and compare V8 vs. any of the above interpreted languages.

share|improve this answer

Once upon a time, BASIC was interpreted. And some variants of BASIC had dynamic typing. And you could get compilers for them as well.

(This was back in the days of 100K floppy drives, when dinosaurs still roamed the earth and ate unsuspecting s/w developers for breakfast.)

share|improve this answer
    
...but only when they used GOTOs. (Which was, of course, quite common if they were developing in BASIC. AHA! That explains it!) –  Mason Wheeler Jul 1 '11 at 2:07
    
BASIC at its design time was a compiled language. –  AProgrammer Jul 1 '11 at 5:15

Different Smalltalk implementations handle this differently, but several of them compile to bytecodes that run on a high-performance VM.

share|improve this answer

Chrome, IE9 and Firefox 3.1+ all compile JavaScript to native binaries, and JavaScript is dynamically typed.

I think the reason that dynamic languages historically tend to be interpreted is because dynamic typing and interpreting (or more specifically, the lack of compiling) both tend to be features useful to scripting languages and scripting tasks in general.

Performance also isn't (wasn't) as much of a concern for the kinds of programs that were written in these languages, so again, the overhead of dynamic typing and interpreting wasn't as big an issue as it would be in languages that value performance.

share|improve this answer

In fact most of the so called "interpreted" languages go through / allow a just-in-time compilation to make it run faster. And some of them has to be compiled to byte code before you can run them.

In fact dynamic and interpreted are totally 2 different ideas, though there is an correlation. The reason being who ever feels the dynamic typing makes their job easier and faster, they wouldn't mind the code to be run a bit slower but portable.

share|improve this answer

No - it's certainly possible to compile dynamic languages.

There are even some dynamic languages that are always compiled by design (e.g. Clojure).

The question however touches on an important related point: although dynamic languages can be compiled, it is often the case that dynamic langauges cannot be compiled down to code that is as efficient as a statically typed language. This is because there are some inherent features in dynamic languages that require runtime checks that would be unnecessary in a statically compiled langauge.

An example of this: languages that allow runtime patching of objects (e.g. Ruby) often require the object to be inspected (with a hashtable lookup or similar) whenever you invoke a method on the object. Even if this is compiled, the compiler will have to generate code to do the method lookup at runtime. To some extent this method lookup is not dissimilar to what an interpreter would have to do.

This adds a significant overhead when compared to a method call in a language like Java, where the correct method can be statically determined by the compiler from the class definition and reduced to a simple function call in native code.

I believe it is this effect more than anything else that results in dynamic languages having slower performance on average than their statically compiled counterparts. As you can see from the flawed benchmarks, it's the statically typed languages (C, Java, Fortran etc.) that tend to be fastest with the dynamic languages (Perl, Python, Ruby, PHP etc.) at the bottom of the ranking.

share|improve this answer

Python is, typically, compiled. Admittedly compiled to byte code that is then interpreted.

Perl works in a similar fashion.

Common Lisp will, typically, compile to one of native or byte code. This differs between implementations (and, to some degree, within an implementation, depending on various optimization settings).

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.