Alternative to psyco for 64-bit machines
Psyco is amazing when it works. However, short of moving to a 32-bit version of Python, it doesn't look like we'll get a 64-bit version anytime soon. Are there any alternatives to psyco that can speed 开发者_如何学Cup CPU-bound general python code?
Related
Psyco x64?
What are the possible pitfalls in porting Psyco to 64-bit?
The answer is to use PyPy instead. From http://psyco.sourceforge.net/introduction.html:
My plans for 2006 are to port the techniques implemented in Psyco to PyPy.
From a PyPy blog post in February:
...highlights and the status of pypy:
* most Python benchmarks run much faster than with CPython or Psyco
* the real-world PyPy compiler toolchain itself (200 KLocs) runs twice as fast
* supports x86 32 and 64bit and is in the process of supporting ARM
...
PyPy, which the Psyco developer points to, is not a handy replacement for Psyco just for the purpose of some speed gain in bottleneck code. Its another ecosystem.
Using PyPy has many implications, raises many compatibility questions for bigger projects and is a pervasive decision: It means switching to a completely different Python implementation far away from the hoods & quality assurance level of the Python leader developers. PyPy is rather over-hyped for its speed gains in most real word projects. Initial executions take long. The memory footprint is huge and ugly. The presented benchmarks are "selected". And there is always some library required for a project, or a GUI / Server / IDE / Debugging issue which quite stops the show, or multiplies development & debug time. The speed gains are hard to predict and hardly can be tuned by focusing. Its nothing to feel well with.
The mature alternatives for speeding up bottleneck code within CPython today are Cython and numba. (There are other "mayflies" here)
Numba, a JIT compiler library for CPython (as is Psyco), focuses on numerical code. It provides impressive speed gains (10..200x, more than psyco / PyPy) just by putting a jit
function decorator on bottleneck functions. And it is particularly swift on and in combination with numpy
arrays out of the box! (unlike Psyco); However so far Numba does nothing for or even slows down tremendously other normal python code, which mainly works on object attributes, strings pieces, general lists, dicts, etc. (Worst seen: 0.04x speed, worse than Psyco). Unlike Psyco, Numba is a huge install, and a slow import (drawing numpy). But it alternatively lets create pre-compiled static modules from selected functions , which then don't need the numba installation any more for deployment (similar to Cython).
Cython, the Python & C kind of mixed language static compiler (a super-set of the Python language), which employs the C-compiler pipeline and produces DLL modules, is usually used by refactoring some bottleneck functions into a separate module. But even when unchanged bigger Python modules are Cython compiled, this typically yields already a speed gain of about 2x, just so "for free"! Yet those compiled DLL modules are significantly bigger than .pyc/.pyo-s. Cython so to say unfolds the Python byte code into direct C-function calls. And it does some little further optimizations on constants etc. (Wheezy.web's pip installation for example by default kind of Cythonizes all its code in a shameless manner without warning - which makes debugging and inspection difficult during development)
In addition to the "free" speed gain of unchanged code, further huge speed gains (approaching C speed), particularly for numeric / array code and some OO-work, can be achieved rather simple way by placing a few Cython static type declarations into hot spot Python code. C speed string processing requires somewhat more involved coding.
Cython allows to continuously improve speed up to C speed by focusing on bottlenecks and tuning the code with Cython types seamlessly in a fine grained manner. Thats a unique strength and has much more potential than Psyco, Numba and PyPy (as it). Its a mature option without a dead end.
精彩评论