PyPy:最热门的话题是什么?

发布于 2024-09-04 03:35:52 字数 303 浏览 7 评论 0原文

注意:标题是故意挑衅的(让你点击它并想要对问题进行投票),我不想看起来很专注。

我已经阅读和听到了越来越多的内容关于 PyPy。它就像一个线性图。

  • 为什么 PyPy 如此特别?据我所知,用语言本身编写的动态语言的实现并不是什么罕见的事情,还是我没有得到一些东西?

  • 有些人甚至将 PyPy 称为“Python 的未来”,或者看到了此实现中的某种深层潜力。这到底是什么意思?

Note: The title is deliberately provocative (to make you click on it and want to close-vote the question) and I don't want to look preoccupied.

I've been reading and hearing more and more about PyPy. It's like a linear graph.

  • Why is PyPy so special? As far as I know implementations of dynamic languages written in the languages itself aren't such a rare thing, or am I not getting something?

  • Some people even call PyPy "the future" [of python], or see some sort of deep potential in this implementation. What exactly is the meaning of this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

﹂绝世的画 2024-09-11 03:35:52

在谈论 PyPy 项目时需要注意的是,它的目标实际上是提供两个可交付成果:第一个是JIT 编译器生成器。是的,生成器,这意味着他们正在实现一个框架,用于编写高度动态的编程语言(例如 Python)的实现。第二个是这个框架的实际测试,是PyPy Python解释器实现

现在,为什么 PyPy 如此特别,有多个答案:该项目开发从 2004 年开始,作为一个研究项目而不是一家公司开始,用 Python 重新实现 Python,用 Python 实现 JIT 编译器,并且可以翻译 RPython(Python 代码)框架能够将该代码转换为 C)编译二进制文件,但存在一些限制。

当前版本的 PyPy 与 CPython 2.5 版本99% 兼容,并且可以运行 Django、Twisted 和许多其他 Python 程序。过去存在无法运行现有 CPython C 扩展的限制,但 PyPy 中的 cpyext 模块也解决了这个问题。 C API 兼容性是可能的,并且在某种程度上已经实现。 JIT也很真实,看这个pystone对比。

使用 CPython:

Python 2.5.5 (r255:77872, Apr 21 2010, 08:44:16) 
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from test import pystone
>>> pystone.main(1000000)
Pystone(1.1) time for 1000000 passes = 12.28
This machine benchmarks at 81433.2 pystones/second

使用 PyPy:

Python 2.5.2 (75632, Jun 28 2010, 14:03:25)
[PyPy 1.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
And now for something completely different: ``A radioactive cat has 18
half-lives.''
>>>> from test import pystone
>>>> pystone.main(1000000)
Pystone(1.1) time for 1000000 passes = 1.50009
This machine benchmarks at 666625 pystones/second

因此,只需在某些计算中使用 PyPy 即可获得近10 倍的加速

因此,随着 PyPy 项目慢慢成熟并提供一些优势,它吸引了试图解决代码速度问题的人们的更多兴趣。 PyPy 的替代方案是 Unladden Sweat(Google 项目),旨在通过使用 LLVM 的 JIT 功能来加速 CPython 的实现,但由于开发人员需要处理 LLVM 中的错误,Unladden Sweat 的进展速度减慢了。

所以,总而言之,我认为 PyPy 被认为是 Python 的未来,因为它将语言规范与 VM 实现分开。例如引入的功能。 stackless Python,然后可以在 PyPy 中实现,只需很少的额外工作,因为它只是改变语言规范,并且您保持共享代码相同。更少的代码、更少的错误、更少的合并、更少的工作。

例如,通过在 RPython 中编写新的 bash shell 实现,您可以免费获得 JIT 编译器并加速许多 Linux shell 脚本,而无需实际学习任何繁重的 JIT 知识。

Good thing to be aware when talking about the PyPy project is that it aims to actually provide two deliverables: first is JIT compiler generator. Yes, generator, meaning that they are implementing a framework for writing implementations of highly dynamic programming languages, such as Python. The second one is the actual test of this framework, and is the PyPy Python interpreter implementation.

Now, there are multiple answers why PyPy is so special: the project development is running from 2004, started as a research project rather than from a company, reimplements Python in Python, implements a JIT compiler in Python, and can translate RPython (Python code with some limitations for the framework to be able to translate that code to C) to compiled binary.

The current version of PyPy is 99% compatible with CPython version 2.5, and can run Django, Twisted and many other Python programs. There used to be a limitation of not being able to run existing CPython C extensions, but that is also being addressed with cpyext module in PyPy. C API compatibility is possible and to some extent already implemented. JIT is also very real, see this pystone comparison.

With CPython:

Python 2.5.5 (r255:77872, Apr 21 2010, 08:44:16) 
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from test import pystone
>>> pystone.main(1000000)
Pystone(1.1) time for 1000000 passes = 12.28
This machine benchmarks at 81433.2 pystones/second

With PyPy:

Python 2.5.2 (75632, Jun 28 2010, 14:03:25)
[PyPy 1.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
And now for something completely different: ``A radioactive cat has 18
half-lives.''
>>>> from test import pystone
>>>> pystone.main(1000000)
Pystone(1.1) time for 1000000 passes = 1.50009
This machine benchmarks at 666625 pystones/second

So you can get a nearly 10x speedup just by using PyPy on some calculations!

So, as PyPy project is slowly maturing and offering some advantages, it is attracting more interest from people trying to address speed issues in their code. An alternative to PyPy is unladden swallow (a Google project) which aims to speed up CPython implementation by using LLVM's JIT capabilities, but progress on unladden swallow was slowed because the developer needed to deal with bugs in LLVM.

So, to sum it up, I guess PyPy is regarded the future of Python because it's separating language specification from VM implementation. Features introduced in, eg. stackless Python, could then be implemented in PyPy with very little extra effort, because it's just altering language specification and you keep the shared code the same. Less code, less bugs, less merging, less effort.

And by writing, for example, a new bash shell implementation in RPython, you could get a JIT compiler for free and speed up many linux shell scripts without actually learning any heavy JIT knowledge.

抚笙 2024-09-11 03:35:52

让我们这样看...
假设您想实现自己的动态语言,并且希望使其速度更快。
你有两个选择:困难的方法和 pypy。

困难的方法意味着用 c 语言编写解释器,然后使用非常复杂的技术(例如方法 jit、线程 jit、跟踪 jit、多态内联缓存、循环不变运动)手动实现 jit(也在 c 中)等等……花了几年的时间来调整它,如果你坚持不懈并且不放弃,你最终可能会得到一个快速的动态语言实现。

或者,您可以使用 pypy 框架。
这意味着,用 python 而不是 c 编写解释器(实际上,它将是 rpython,一个可以编译为 c 的更有限的 python 子集)。
一旦你编写了解释器,pypy 将自动免费生成 jit。
你基本上就完成了。

听起来不错吧?

Lets see it this way...
Suppose you want to implement your own dynamic language, and you want to make it fast.
You have two options: the hard way and pypy.

The hard way means writing your interpreter in c, and then implement a jit by hand, also in c, by using a mix of very complicated techniques such a method-jit, threaded-jit, tracing jit, polymorphic inline caches, loop invariant motion, etc, etc... Spent several years tuning it up and if you persevere a lot and you don't give up, you may end up with a fast dynamic language implementation.

Or, you can use the pypy framework.
That means, writing your interpreter in python instead of c (actually, it would be rpython, a more limited subset of python that can be compiled to c).
Once you wrote your interpreter, pypy will automatically generate a jit for free.
And you are basically done.

Sounds good, huh?

原谅我要高飞 2024-09-11 03:35:52

PyPy 的酷之处在于(除了速度快并且是用 RPython(Python 语言的子集)编写的,基本上是自举的之外,它还可以为任何对象提供自动创建的 JIT(即时编译器)) 并且速度相当快。

您用 PyPy 编写的程序:这使得它非常适合快速实现您自己的语言 , /pypy/doc/jit/overview.html" rel="noreferrer">此处

The cool thing about PyPy (aside from being fast and written in RPython (a subset of the Python language) so basically bootstrapped, is that it can provide an automatically created JIT (just in time compiler) for any program you write in PyPy: this makes it ideal to implement, quickly, your own language and have it be rather fast.

Read more here

热风软妹 2024-09-11 03:35:52

更不用说他们最近在某些基准测试中超过了 CPython 的速度。我想,请参阅他们的博客。我无法从这里访问它:

http://morepypy.blogspot.com/

Not to mention that they just recently exceeded the speed of CPython on some benchmarks. See their blog, I think. I can't reach it from here:

http://morepypy.blogspot.com/

过期情话 2024-09-11 03:35:52

由于我们大多数人都认为编写 Python 比 C 更容易,因此用 Python(好吧,技术上是 RPython)编写的 Python 解释器应该比 CPython 更容易修改,并且错误更少。

Since most of us agree that it's easier to write Python than C, a Python interpreter that's written in Python (well, technically RPython) should be able to be modified much easier and with less bugs than CPython.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文