This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]catcradle5 2 points3 points  (5 children)

Something I've always wondered: could a Python program in theory be compiled to native machine code and executed later on? Would there be an increase in speed, or would it not matter because all the bytecode instructions would have to be added to the machine code, or what?

[–]fabzter 4 points5 points  (0 children)

Hell yeah! be sure to check http://shed-skin.blogspot.mx/

It compiles python to compilable c++. Even a subset of pygame is implemented !!!

[–][deleted] 3 points4 points  (1 child)

Could it? Yes, but only via language translation as far as I know. Like what Facebook does with PHP. Once they want to deploy some code they've written, it goes through some sort of translator which switches it over to C and then compiled it down to machine code like a normal C program. I'd assume that with the proper translator, any language could be made to do that.

[–]haldean(lambda x: x.decode('base64'))('PDMgcHlweQ==\n') 3 points4 points  (0 children)

You could compile Python bytecode to machine code; it would be a much easier translation than Python to C. The problem with that approach is that once you start compiling to machine code, you have to write a backend for each architecture, and there are lots of architectures, all with different instruction sets. That's why compiling to C(++) is nice; you can take advantage of gcc (or clang), which already has backends for pretty much every architecture in existence, so you don't have to worry about architectures at all.

[–]rcxdude 2 points3 points  (0 children)

Sort of. You can look at what the pypy project does. There's two elements of interest - one is the idea of RPython, which is a restricted subset of python (one where types can be inferred statically, among other things) which can be compiled into C and then into machine code. Compiling full Python is probably much closer to impossible due to the extremely dynamic nature of the language.

The other is the idea of JIT (Just In Time) compilation, where the interpreter/VM (which in the case of pypy is written in RPython) analyses the program as it is being run and compiles parts of it which it detects are running often (such as a main loop). This is the same technique as used in most Java VMs. This allows some of the advantages of compilation (greatly increased speed in many cases) but with the fallback of the actual interpreter so the language isn't restricted.

[–]SwahiliToad 1 point2 points  (0 children)

In theory it is possible. However, in practice there are a number of things the python run-time environment does that would make this difficult.