Slashdot Mirror


Python-to-C++ Compiler

Mark Dufour writes "Shed Skin is an experimental Python-to-C++ compiler. It accepts pure, but implicitly statically typed, Python programs, and generates optimized C++ code. This means that, in combination with a C++ compiler, it allows for translation of pure Python programs into highly efficient machine language. For a set of 16 non-trivial test programs, measurements show a typical speedup of 2-40 over Psyco, about 12 on average, and 2-220 over CPython, about 45 on average. Shed Skin also outputs annotated source code."

12 of 181 comments (clear)

  1. not terribly useful quite yet by Surt · · Score: 4, Insightful

    Until he addresses mixed types in n-tuples, this won't be useful for very many people.

    --
    "Who is the Journal of Quantum Physics going to believe?" --Stephen Hawking
  2. Native code by Roy+van+Rijn · · Score: 3, Insightful

    This is a good step to make Python run a bit faster, but I don't think it'll really make a huge difference.

    The best way to get some speed and still keep the nice Python functions and layout is just to export the most heavily used functions to native code (C/C++).
    I don't know if its possible to take the C++ output and optimize it seperatly, that way you will have a good start to make native code though.

    In short: Better, fast and easy, but not the best (if you can write native code)

  3. Re:Ewwwww by Anonymovs+Coward · · Score: 5, Insightful
    Completely unreadable.

    I think you're not supposed to read it. You're only supposed to feed it to your C++ compiler. f2c produced unreadable output too, but nobody read the output; at one time it was the only free fortran option on linux.

  4. Re:Sounds good... by B'Trey · · Score: 3, Insightful

    I will have to explore it more, but it will be intriguing to see how they handle things like pointers and structs that are not in python.

    Uh, why would they have to? This goes from Python to C++, not vice versa. If there are no pointers or structs in the Python code, why would they have to handle them? Certainly, it's quite possible that some Python variable types will be converted to pointers or structs in the output code, but that's orthagonal to the issue of Python not having them natively.

    If you were trying to go from C++ to Python, then you'd have to convert C++ pointers and structs to some sort of Python data type, and your comment would make sense. As it is, I'm not sure what you were trying to say.

    --

    "The legitimate powers of government extend only to such acts as are injurious to others." Thomas Jefferson.

  5. Re:Yeah, but that's not what we need. by Anonymovs+Coward · · Score: 3, Insightful

    I don't see your point. Some of us use python. It takes me a fraction the time to do something in python than to do it in any other language. I'm not interested in writing native C++ code because it's hypothetically faster (it's not faster if I count coding time). But I am interested in a good python-to-C++ translator. Why wouldn't any python user be?

  6. Re:Yeah, but that's not what we need. by advocate_one · · Score: 4, Insightful
    But I am interested in a good python-to-C++ translator. Why wouldn't any python user be?

    no, I'd be far more interested in a good compiler to compile that python straight to machine code...

    --
    Donald 'Duck' Dunn: We had a band powerful enough to turn goat piss into gasoline.
  7. File as NBNC (Nice But No Cigar) by suitepotato · · Score: 4, Insightful

    Why? Read the linked page? Says it all. Violates most any Python code of any complexity out there. So if it doesn't convert Python code from the real world, what is it for? Making Python coders learn enough about C++ to remember the limitations and write/rewrite Python code to use it?

    What the Python C/C++ interested people REALLY need is a book written by a group of Python AND C/C++ masters which teaches the two simultaneously showing complimentary methods of doing any given thing working from beginner to advanced and I DON'T mean "How to turn your n00b Python code into C/C++ hotness" sort of viewpoint. I mean both taught simultaneously in synch showing how they can interchange and compliment.

    Software tricks for converting? Ultimately worse than not having them because it leads to horrible obfuscation because we don't know exactly what is going on when 13,412 lines of Python is turned into C++ because WE DIDN'T WRITE IT AND WE NEVER LEARNED C/C++. "Say Mike, that's great but you're the company code cowboy and you don't do C++ natively and I sure as hell don't read it being management so exactly what happens if this needs to be fixed? We've gone from importing open source code you couldn't read to writing our own open source code you can't read."

    --
    If my grammar and spelling are off, I am [distracted/tired/careless] (take your pick)
    1. Re:File as NBNC (Nice But No Cigar) by try_anything · · Score: 3, Insightful
      Software tricks for converting? Ultimately worse than not having them because it leads to horrible obfuscation because we don't know exactly what is going on when 13,412 lines of Python is turned into C++ because WE DIDN'T WRITE IT AND WE NEVER LEARNED C/C++. "Say Mike, that's great but you're the company code cowboy and you don't do C++ natively and I sure as hell don't read it being management so exactly what happens if this needs to be fixed?"

      That isn't how a compiler is used. When you compile a C++ program, you don't throw away your C++ source and check the executable into source control. "Oh, no! We used gcc and now we have a bunch of gobbledygook we don't understand!"

      The C++ is an intermediate stage in the make process, akin to the output of various phases of gcc.

  8. Re:If they can do this... by rpwoodbu · · Score: 5, Insightful

    It is worth mentioning that one of the the original implementations of C++ (if not the very first) was "cfront", a C++-to-C converter. I see this as a much easier way to get a new language implemented quickly, as you can take advantage of the common functionalities already implemented in the target language of the converter. Although Python is not a new language, using it as a compiled language is new, and thus I believe it is comparable to being a new language for this argument. C++ and Python have a lot in common, which makes C++ a very suitable target language for a Python-to-[compiled_language] converter.

    If this converter proves to be successful, I believe that a GCC frontend will be written eventually. There are probably potential optimizations that would be difficult or impossible to implement any other way.

    Some may think that the dynamic nature of Python may preclude its inclusion in GCC. Technically, all that would need to be done is to have a runtime to handle dynamic things, similar to how Objective-C (for which there is GCC support) has a runtime to handle message passing and late binding. However, a large portion of the potential efficiency of a compiled version of the language would be lost to these dynamic capabilities; luckily, a compiler can detect when things are implicitly static (in fact, this converter is limited to implicitly static constructs), and optimise them to be truly static at compile-time.

  9. Stupid comparison by ardor · · Score: 3, Insightful

    As another poster already said, file I/O is a bottleneck regardless of ANY language. So, try something different. Real-time h264 decoding for example.

    --
    This sig does not contain any SCO code.
  10. Re:Yeah, but that's not what we need. by mrchaotica · · Score: 3, Insightful

    ...and that's why it shouldn't be a Python to C++ translator; it should be a GCC frontend instead (i.e., translating to GCC's internal representation).

    --

    "[Regarding the 'cloud,'] ownership was what made America different than Russia." -- Woz

  11. Re:Ewwwww by Tim+Browse · · Score: 3, Insightful

    Yeah, whenever I look at the output of my optimising compiler, it's really hard to understand too. It's all in assembler, for a start.

    Plus, the quality of C code generated by CFront was rubbish - unreadable.

    Same with the Modula-3 compiler I tried. You couldn't work out what was going on in the resulting C code without a load of work.

    Can you see where I'm going with this?