为 PyPy 的 JIT 编写快速代码的指南

发布于 2024-10-22 06:54:09 字数 447 浏览 2 评论 0原文

PyPy 的 JIT 可以使 Python 代码的执行速度比 CPython 快得多。是否有一套编写可以通过 JIT 编译器更好优化的代码的指南?例如,Cython 可以将一些静态代码编译为 C++,并且它具有编写高效代码的指南。 PyPy 有一套好的实践吗?我知道 PyPy 项目有在编写您自己的支持 JIT 时包含提示的指南其他动态语言的解释器,但这与框架的大多数最终用户无关,他们只是使用解释器。我想知道的问题包括:

  1. 将脚本打包到函数中
  2. 显式删除变量
  3. 给出或暗示变量类型的可能方法
  4. 以某种方式编写循环

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:

  1. Packaging a script into functions
  2. Explicitly deleting variables
  3. Possible ways of giving, or hinting variable types
  4. Writing loops a certain way

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

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

发布评论

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

评论(1

坚持沉默 2024-10-29 06:54:09

BitBucket 上的 PyPy wiki 有一个关于 JIT 友好性。一些博客文章提供关于使代码在 PyPy 中快速运行的进一步建议,但 AFAIK 的想法是,不强制解释/实现框架的惯用代码应该是快速且如果不是的话,就是一个错误。

我知道对于 3,一些“assert x > 0”或类似的语句可能有用,但我不记得在哪里看到的。我还相信我已经看到了一些关于重构conditional-paths-in-loops的建议与4相关(编辑:这似乎是过时的 现在)。

这是包含一些相关讨论的线程。您可以使用 jitviewer 检查 JIT 与代码的配合情况,但是有点先进了。在 Freenode 上加入 #pypy 将为您提供有关 jitviewer 和您的特定代码的帮助。

2020+

自 Pypy 移至 2020 年的 HeptapodJIT 友好性已移至此处: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

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