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 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:
|
|||||||||||||||||||||
|