来自谷歌开源博客:

PyPy是Python的重新实现 在Python中,使用高级技术 努力获得更好的表现 比CPython的。多年的辛勤工作 终于有了回报。我们的速度 结果经常击败CPython,测距 从稍微慢一点,到 实车加速可达2倍 应用程序代码,以加速高达 10倍于小型基准。

这怎么可能呢?哪个Python实现被用来实现PyPy?CPython的吗?PyPyPy或PyPyPyPy超过他们分数的几率有多大?

(与此相关的是……为什么会有人尝试这样的事情?)


当前回答

Q1。这怎么可能呢?

在某些情况下,手动内存管理(这是CPython对其计数所做的)可能比自动管理慢。

CPython解释器实现的局限性排除了PyPy可以做的某些优化(例如。细粒度锁)。

正如马塞洛提到的,JIT。能够动态地确认对象的类型可以使您不必执行多个指针解引用来最终到达想要调用的方法。

Q2。哪个Python实现被用来实现PyPy?

PyPy解释器是在RPython中实现的,RPython是Python(语言而不是CPython解释器)的静态类型子集。—详见https://pypy.readthedocs.org/en/latest/architecture.html。

第三季。PyPyPy或PyPyPyPy超过他们分数的几率有多大?

这将取决于这些假设解释器的实现。例如,如果他们中的一个人获得源代码,对其进行某种分析,并在运行一段时间后将其直接转换为严格的目标特定的程序集代码,我想它会比CPython快得多。

更新:最近,在一个精心设计的示例中,PyPy的性能超过了用gcc -O3编译的类似C程序。这是一个虚构的案例,但确实展示了一些想法。

第四季度。为什么会有人这么做?

来自官方网站。https://pypy.readthedocs.org/en/latest/architecture.html#mission-statement

We aim to provide: a common translation and support framework for producing implementations of dynamic languages, emphasizing a clean separation between language specification and implementation aspects. We call this the RPython toolchain_. a compliant, flexible and fast implementation of the Python_ Language which uses the above toolchain to enable new advanced high-level features without having to encode the low-level details. By separating concerns in this way, our implementation of Python - and other dynamic languages - is able to automatically generate a Just-in-Time compiler for any dynamic language. It also allows a mix-and-match approach to implementation decisions, including many that have historically been outside of a user's control, such as target platform, memory and threading models, garbage collection strategies, and optimizations applied, including whether or not to have a JIT in the first place.

C编译器gcc是用C语言实现的,Haskell编译器GHC是用Haskell编写的。你有什么理由不让Python解释器/编译器用Python编写吗?

其他回答

PyPy是用Restricted Python编写的。据我所知,它并不运行在CPython解释器之上。Restricted Python是Python语言的一个子集。AFAIK, PyPy解释器被编译为机器码,因此在安装时它不会在运行时使用python解释器。

你的问题似乎期望PyPy解释器在执行代码时运行在CPython之上。 编辑:是的,要使用PyPy,首先要将PyPy的python代码转换为C并使用gcc构建,转换为jvm字节代码,或转换为。net CLI代码。参见开始

“PyPy是Python中的Python的重新实现”是一种相当具有误导性的描述PyPy的方式,恕我直言,尽管它在技术上是正确的。

PyPy有两个主要部分。

翻译框架 解释器

翻译框架是一个编译器。它将RPython代码编译成C(或其他目标),自动添加垃圾收集和JIT编译器等方面。它不能处理任意的Python代码,只能处理RPython。

RPython is a subset of normal Python; all RPython code is Python code, but not the other way around. There is no formal definition of RPython, because RPython is basically just "the subset of Python that can be translated by PyPy's translation framework". But in order to be translated, RPython code has to be statically typed (the types are inferred, you don't declare them, but it's still strictly one type per variable), and you can't do things like declaring/modifying functions/classes at runtime either.

这个解释器就是一个用RPython编写的普通Python解释器。

因为RPython代码是普通的Python代码,所以可以在任何Python解释器上运行它。但PyPy声称的速度都不是来自于这样运行;这只是为了快速测试周期,因为翻译解释器需要很长时间。

了解了这一点,就应该立即明白,关于PyPyPy或PyPyPy的推测实际上没有任何意义。你有一个用RPython编写的解释器。您可以将其转换为C代码,从而快速执行Python。这个过程在那里停止;没有更多的RPython来加速再次处理它。

所以“PyPy怎么可能比CPython快”也变得相当明显。PyPy有一个更好的实现,包括一个JIT编译器(我认为,如果没有JIT编译器,它通常没有那么快,这意味着PyPy只对容易受到JIT编译影响的程序更快)。CPython从未被设计为Python语言的高度优化实现(尽管他们确实试图使其成为高度优化的实现,如果你了解差异的话)。


The really innovative bit of the PyPy project is that they don't write sophisticated GC schemes or JIT compilers by hand. They write the interpreter relatively straightforwardly in RPython, and for all RPython is lower level than Python it's still an object-oriented garbage collected language, much more high level than C. Then the translation framework automatically adds things like GC and JIT. So the translation framework is a huge effort, but it applies equally well to the PyPy python interpreter however they change their implementation, allowing for much more freedom in experimentation to improve performance (without worrying about introducing GC bugs or updating the JIT compiler to cope with the changes). It also means when they get around to implementing a Python3 interpreter, it will automatically get the same benefits. And any other interpreters written with the PyPy framework (of which there are a number at varying stages of polish). And all interpreters using the PyPy framework automatically support all platforms supported by the framework.

因此,PyPy项目的真正好处是(尽可能多地)分离出为动态语言实现高效的平台独立解释器的所有部分。然后在一个地方提出一个好的实现,可以在许多解释器中重复使用。这不是像“我的Python程序现在运行得更快了”那样的立竿见影的胜利,但它是未来的一个美好前景。

并且它可以更快地运行你的Python程序(也许)。

Q1。这怎么可能呢?

在某些情况下,手动内存管理(这是CPython对其计数所做的)可能比自动管理慢。

CPython解释器实现的局限性排除了PyPy可以做的某些优化(例如。细粒度锁)。

正如马塞洛提到的,JIT。能够动态地确认对象的类型可以使您不必执行多个指针解引用来最终到达想要调用的方法。

Q2。哪个Python实现被用来实现PyPy?

PyPy解释器是在RPython中实现的,RPython是Python(语言而不是CPython解释器)的静态类型子集。—详见https://pypy.readthedocs.org/en/latest/architecture.html。

第三季。PyPyPy或PyPyPyPy超过他们分数的几率有多大?

这将取决于这些假设解释器的实现。例如,如果他们中的一个人获得源代码,对其进行某种分析,并在运行一段时间后将其直接转换为严格的目标特定的程序集代码,我想它会比CPython快得多。

更新:最近,在一个精心设计的示例中,PyPy的性能超过了用gcc -O3编译的类似C程序。这是一个虚构的案例,但确实展示了一些想法。

第四季度。为什么会有人这么做?

来自官方网站。https://pypy.readthedocs.org/en/latest/architecture.html#mission-statement

We aim to provide: a common translation and support framework for producing implementations of dynamic languages, emphasizing a clean separation between language specification and implementation aspects. We call this the RPython toolchain_. a compliant, flexible and fast implementation of the Python_ Language which uses the above toolchain to enable new advanced high-level features without having to encode the low-level details. By separating concerns in this way, our implementation of Python - and other dynamic languages - is able to automatically generate a Just-in-Time compiler for any dynamic language. It also allows a mix-and-match approach to implementation decisions, including many that have historically been outside of a user's control, such as target platform, memory and threading models, garbage collection strategies, and optimizations applied, including whether or not to have a JIT in the first place.

C编译器gcc是用C语言实现的,Haskell编译器GHC是用Haskell编写的。你有什么理由不让Python解释器/编译器用Python编写吗?

PyPy是用Python实现的,但它实现了一个JIT编译器来动态生成本机代码。

在Python之上实现PyPy的原因可能只是因为它是一种非常高效的语言,特别是因为JIT编译器使宿主语言的性能有些无关紧要。