You are likely referring to CPython, the standard Python implementation, which is a bytecode interpreter written in C. Python The Language is a somewhat ill-specified syntax and set of behaviors that come from the documentation. Python as a language must be held apart from Python implementations. X-posted from my comment in r/programminglanguages:
Or if it's the syntax of Python that you really like, then that might be an interesting transpiler project for you: turn what looks like Python code into C or some other faster language that you favour.
When the balance is correct, your application shouldn't appear significantly slowed down compared to 100% native code. There are various ways of make those work together. If you want to stay with Python, then you need to program using a hybrid approach of mixing Python with critical code written in a language that is properly compiled, typically C. Or something like Nim which has Python-like features but is compiled, via C, to native code. If you just want a faster language that stays fairly easy to code in, try a different scripting language. Others may restrict the language, or may require static type information, or turn it into something else. That hasn't stopped people trying, and there are myriad projects aimed at making Python faster. It also has eval() and exec() which allow execution of code not known until runtime. Things which are declarations in other languages, are conditional statements here. Python makes it harder still by having nearly everything dynamic and liable to change from one line to the next. A language with dynamic types is hard to compile into native code, which I assume is what you mean (rather than to bytecode). Is it even possible to make a compiler for Python? That's the price you pay for this kind of language design.
Unfortunately, dynamically looking up the types of 2 objects to decide how to add them together, even with state of the art compilation, is easily 10x to 100x slower than just adding 2 machine word-sized integers together and stuffing the result in a machine register. It does this in C, using modern state of the art C compilers. So Python has to figure out the type of something every time you perform an operation on it. Since the names of those arguments can be dynamically rebound to a different object (with a different type) at basically any time, from basically anywhere in the program, it's very hard to make any assumptions about what something is until you try. Every time you add 2 objects together, Python has to figure out what type the arguments are and then apply the correct addition operation to them. The problem is that Python has to dynamically resolve the type of basically everything, for every operation, every time you perform it. The issue isn't the bytecode interpreter loop (which is the easiest part to get rid of in a Python compiler project). Python is built in C already, through and through, it's already optimized, and it's already fast at what it does. Nuitka is a good example (also a good way to make a single file executable). But TBF, any Python compiler working as I suggested would have trouble trying to speed up programs using complex, variant datatypes. This may give a variable benefit on real applications as opposed to benchmarks. Is there such a program that works like this? Pypy as I said can be used as a drop-in: pypy fib.py # JIT/interpreterįib(46) = 1836311903 # took 24 seconds iirc Pythonc fib.py # compiler produces fib.exeįib(46) = 1836311903 # takes 10 seconds say
This is how I would hope a Python compiler would work: python fib.py # normal interpreterįib(46) = 1836311903 # takes 500 seconds approx py programs unchanged.Īs far as I can see, Cython doesn't even produce an executable that you can run like any regular program, although there's likely to be a separate add-on that can do that. It's not a drop-in replacement for python.exe as pypy.exe for example might be, which runs. But you have to write in a superset of Python to get any benefit.Īnd the way you run programs is rather more elaborate too. If it was that simple then everyone would be using this.