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.

As I understand, the cause of the speed difference between compiled languages and python is, that the first compiles code all way to the native machine's code, whereas python compiles to python bytecode, to be interpreted by the PVM. I see that this way python codes can be used on multiple operation system (at least in most cases), however I do not understand, why is not there an additional (and optional) compiler for python, which compiles the same way as traditional compilers. This would leave to the programmer to chose, which is more important to them; multiplatform executability or performance on native machine. In general; why are not there any languages which could be behave both as compiled and interpreted?

share|improve this question
4  
There is. Haskell can also behave as compiled or interpreted through GHCI –  toasted_flakes Jun 7 '14 at 10:00
    
C++ also have interpreters. And probably tons of other languages have both implementations. –  Claudio Jun 7 '14 at 11:29
1  
Actually, by choosing IronPythong (ironpython.net) and compile the produced IL code by using "ngen" (msdn.microsoft.com/de-de/library/6t9t5wcf%28v=vs.110%29.aspx) there is a way to compile Python to native machine code. Not that I had tested that tool chain. –  Doc Brown Jun 7 '14 at 13:00
4  
One can write Python-to-native compilers. They just aren't very interesting because they don't actually improve performance by any significant margin, unless they actually implement a language that looks like Python but is far more restricted. I previously explained elsewhere why. –  delnan Jun 7 '14 at 17:51

1 Answer 1

up vote 11 down vote accepted

No. The reason why there is speed difference between languages like Python and C++ is because statically-typed languages give compiler tons of information about structure of the program and data which allows it to optimize both computations and memory access. Because C++ knows that variable is of type int, it knows optimal way to manipulate that variable even before the program is run. In Python on the other way, the runtime doesn't know what value is in variable right until the line is reached by the interpreter. This is extremely important for structures, where in C++, the compiler can easily tell the size of the structure and every location of it's field within a memory during compilation. This give is huge power in predicting how the data might be used and is able to optimize it according to those predictions. No such thing is possible for language like Python.

To effectively compile language like Python you would need to:

  1. Ensure that structure of data is static during the execution of the program. This is problematic because Python has eval and metaclasses. Both which make it possible to change the structure of the program based on the input of the program. This is one of the things that give Python such expressive power.
  2. Infer the types of all variable, structures and classes from the source code itself. While it is possible to some degree, the static type system and algorithm would be so complex it would be almost impossible to implement in a usable way. You could do it for subset of language, but definitely for not whole set of language features.
share|improve this answer
1  
It's worth noting that this does make the problem hard, but not impossible. sbcl compiles Common Lisp which also is dynamic, has eval, and a bunch of other things to make compiler writers sad. It's not to the level of gcc, but it's certainly faster than CPython's interpreter. –  jozefg Jun 7 '14 at 16:44
1  
@jozefg I said effectively compile. Not just compile. Python too has it's Cython compiler which produces native code. The point is those compilers ca'nt do even fraction of optimizations that compilers for statically-typed languages can. And when you compare performance, compare it to compiled C++ and not interpreted Python. –  Euphoric Jun 7 '14 at 16:47
1  
Well actually, you'd be surprised what sbcl can do. The benchmarks game shows it running as fast as Java, nearly as fast as GHC, and within 1x to 10x of C. It's not slow by any standards. Yes, dynamic types inhibit compilation to some extent, but not so much as you seem to think. –  jozefg Jun 7 '14 at 16:52
    
@jozefg this depends on the nature of the application and the implementation. If I were to code in C++ using Python philosophy (e.g. use lists for just about anything), the performance of my C++ program would be terrible (or, conversely, Python would be competitive). A proper static implementation may have significant performance benefits over a dynamic one, depending on the nature of the program (for IO bound programs it won't matter, for instance). Note that performance benefits need not be time: memory requirements are also much higher for dynamicly typed languages. –  Marc Claesen Jul 30 '14 at 14:07
1  
Comparing the speed of interpreted python to compiled python is interesting in itself. Stop saying "use C++". Perhaps you already have the code written in Python. Perhaps the code is easier to write in python. Who cares. What I do care about is a 1.5x speed up (whatever it is). That can make a huge difference. –  Thomas Eding Nov 8 '14 at 10:30

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.