我听说了很多关于PyPy项目的事情。他们声称它比他们网站上的CPython解释器快6.3倍。

每当我们谈论像Python这样的动态语言时,速度是首要问题之一。为了解决这个问题,他们说PyPy要快6.3倍。

第二个问题是并行性,即臭名昭著的全局解释器锁(GIL)。为此,PyPy说它可以提供无gil的Python。

如果PyPy可以解决这些巨大的挑战,那么它的弱点是什么?也就是说,是什么阻止了像我这样的典型的Python开发人员现在转向PyPy ?


当前回答

I did a small benchmark on this topic. While many of the other posters have made good points about compatibility, my experience has been that PyPy isn't that much faster for just moving around bits. For many uses of Python, it really only exists to translate bits between two or more services. For example, not many web applications are performing CPU intensive analysis of datasets. Instead, they take some bytes from a client, store them in some sort of database, and later return them to other clients. Sometimes the format of the data is changed.

BDFL和CPython开发人员是一群非常聪明的人,他们设法帮助CPython在这种情况下表现出色。这是一个无耻的博客:http://www.hydrogen18.com/blog/unpickling-buffers.html。我使用的是Stackless,它源自CPython,并保留了完整的C模块接口。在这种情况下,我没有发现使用PyPy的任何优势。

其他回答

注意:与2013年提出这个问题时相比,现在的PyPy更成熟,支持也更好。避免从过时的信息中得出结论。


PyPy, as others have been quick to mention, has tenuous support for C extensions. It has support, but typically at slower-than-Python speeds and it's iffy at best. Hence a lot of modules simply require CPython. PyPy doesn't support numpy. Some extensions are still not supported (Pandas, SciPy, etc.), take a look at the list of supported packages before making the change. Note that many packages marked unsupported on the list are now supported. Python 3 support is experimental at the moment. has just reached stable! As of 20th June 2014, PyPy3 2.3.1 - Fulcrum is out! PyPy sometimes isn't actually faster for "scripts", which a lot of people use Python for. These are the short-running programs that do something simple and small. Because PyPy is a JIT compiler its main advantages come from long run times and simple types (such as numbers). PyPy's pre-JIT speeds can be bad compared to CPython. Inertia. Moving to PyPy often requires retooling, which for some people and organizations is simply too much work.

我想说,这些是影响我的主要原因。

我发现了一些例子,其中PyPy比Python慢。 但是:只在Windows上。

C:\Users\User>python -m timeit -n10 -s"from sympy import isprime" "isprime(2**521-1);isprime(2**1279-1)"
10 loops, best of 3: 294 msec per loop

C:\Users\User>pypy -m timeit -n10 -s"from sympy import isprime" "isprime(2**521-1);isprime(2**1279-1)"
10 loops, best of 3: 1.33 sec per loop

所以,如果你想到PyPy,忘了Windows吧。 在Linux上,你可以实现惊人的加速。 示例(列出1到1,000,000之间的所有质数):

from sympy import sieve
primes = list(sieve.primerange(1, 10**6))

这在PyPy上比在Python上快10(!)倍。 但不是在窗户上。那里的速度只有原来的3倍。

CPython有引用计数和垃圾收集,PyPy只有垃圾收集。

因此,对象倾向于更早地删除,并且在CPython中以更可预测的方式调用__del__。有些软件依赖于这种行为,因此它们还没有准备好迁移到PyPy。

其他一些软件可以同时使用这两种方法,但使用CPython时使用的内存更少,因为未使用的对象会更早释放。(我没有任何测量方法来表明这有多重要,以及其他哪些实现细节会影响内存使用。)

对于许多项目,不同的python之间在速度方面实际上是0%的差异。这是由工程时间主导的,所有的python都有相同数量的库支持。

支持的Python版本

引用Python的禅意:

可读性。

例如,Python 3.8引入了fstring =。

Python 3.8+中可能有其他对您更重要的特性。PyPy目前不支持Python 3.8+。

无耻的自我广告:Python版本的杀手特性-如果你想知道更多你使用旧Python版本时错过的东西