为 PyPy 的 JIT 编写快速代码的指南
PyPy 的 JIT 可以使 Python 代码的执行速度比 CPython 快得多。是否有一套编写可以通过 JIT 编译器更好优化的代码的指南?例如,Cython 可以将一些静态代码编译为 C++,并且它具有编写高效代码的指南。 PyPy 有一套好的实践吗?我知道 PyPy 项目有在编写您自己的支持 JIT 时包含提示的指南其他动态语言的解释器,但这与框架的大多数最终用户无关,他们只是使用解释器。我想知道的问题包括:
- 将脚本打包到函数中
- 显式删除变量
- 给出或暗示变量类型的可能方法
- 以某种方式编写循环
PyPy's JIT can make Python code execute much faster than CPython. Are there a set of guidelines for writing code that can be optimised better by the JIT compiler? For example, Cython can compile some static code into C++, and it has guidelines to write efficient code. Are there a set of good practices for PyPy? I know that the PyPy project has guidelines for including hints while writing your own JIT-enabled interpreters for other dynamic languages, but that is not relevant to most end users of the framework, who are simply using the interpreter. Questions I am wondering about include:
- Packaging a script into functions
- Explicitly deleting variables
- Possible ways of giving, or hinting variable types
- Writing loops a certain way
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
BitBucket 上的 PyPy wiki 有一个关于 JIT 友好性。一些博客文章提供关于使代码在 PyPy 中快速运行的进一步建议,但据我所知,这个想法是惯用的代码不强制解释/实现帧应该很快,如果不是,那就是一个错误。
我知道对于 3,一些“assert x > 0”或类似的语句可能有用,但我不记得在哪里看到的。我还相信我已经看到了一些关于重构conditional-paths-in-loops的建议与 4 相关(编辑:这似乎是 现在已过时)。
这是包含一些相关讨论的线程。您可以使用 jitviewer 检查 JIT 与代码的配合情况,但是有点先进了。在 Freenode 上加入 #pypy 将为您提供有关 jitviewer 和您的特定代码的帮助。
2020+
自 Pypy 移至 2020 年七肢桶,JIT 友好度发生了变化这里: https://foss.heptapod.net/pypy/pypy/-/ wikis/JitFriendiness
此处提供了其他性能信息:https://www.pypy.org/performance.html
PyPy wiki's at BitBucket has a section on JIT Friendliness. Some blog posts offer further advice on making code run fast in PyPy, but AFAIK the idea is that idiomatic code that doesn't force interpreting/realizing frames should be fast and is a bug if it isn't.
I know that for 3, some "assert x > 0" or similar statements can be useful, but I don't remember where I saw that. I also believe I've seen some suggestion about refactoring conditional-paths-in-loops related to 4 (edit: this seems to be outdated now).
Here's a thread with some related discussion. You can check how well the JIT is working with your code with jitviewer, but it's somewhat advanced. Joining #pypy on Freenode will get you help with jitviewer and your particular code.
2020+
Since Pypy moved to Heptapod in 2020, the JIT Friendliness has moved here: https://foss.heptapod.net/pypy/pypy/-/wikis/JitFriendliness
Additional performance info is available here: https://www.pypy.org/performance.html