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?
No. The reason why there are speed differences between languages like Python and C++ is because statically-typed languages give the compiler tons of information about the structure of the program and its data which allows it to optimize both computations and memory access. Because C++ knows that variable is of type int, it can determine the optimal way to manipulate that variable even before the program is run. In Python on the other hand, the runtime doesn't know what value is in a 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 its fields within memory during compilation. This gives it huge power in predicting how the data might be used and lets it optimize according to those predictions. No such thing is possible for languages like Python.
To effectively compile languages like Python you would need to:
- Ensure that the 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.
- Infer the types of all variables, 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 a subset of the language, but definitely not for the whole set of language features.
Two concepts might help us understand better why Python compiled to native machine code "may" not run as fast as compiled C or other commonly compiled languages. They are called early binding and late binding.
I should begin by saying I am not a Python expert, and I came to this site by accident. But I like this site.
As mentioned in another response here, the C++ compiler can know a lot about the program and make decisions about which operations to use for specific data structures. As an example if two integer variables need to be added together, the compiler knows they are native integers, 32 bits wide for example and it can add them together with one "ADD" instruction. So it compiles the ADD instruction into the code. It's locked in and can't be changed while the program is running. That is early binding.
On the other hand in a language like Python we could expect the program to throw different types of data together in complex ways. Now the compiler does not know if our 2 variables are integers, floats, strings or lists. So it has to compile code that determines that information at run time and select the correct operation while the program is running. This is late binding and we can understand that there will be a performance hit for doing that extra work while the program is running. It is the price you pay for keeping those options open in a language like Python but it provides maximum run-time flexibility.
I think it has more to do with the Python specifics itself, the same reason you can't compile C# to machine code. Language specifics would actually render your programs buggy even if it were possible due to the nature of the language. Why not just learn the C language? Its much easier than C++ and slightly advanced than Python but still approachable.
protected by gnat Feb 6 '16 at 17:22
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?