为PyPy的JIT编写快速代码的指南 [英] Guidelines to write fast code for PyPy's 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
推荐答案
PyPy wiki在BitBucket 有一个部分上 JIT友善。有些博客提供进一步的建议,使代码在PyPy中快速运行,但AFAIK的想法是,不强制解释/实现框架的惯用代码应该是快速的,如果它是一个错误't。
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.
我知道对于3,一些assert x> 0或类似语句可能有用,但我不记得我在哪里看到了。我也相信我看到了有关重构与4相关的条件路径循环的一些建议(编辑:这似乎是现在已过时)。
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).
这里有一个一些相关讨论。您可以使用 jitviewer 检查JIT与代码的工作情况,但它有点高级。在Freenode上加入#pypy将帮助你使用jitviewer和你的代码。
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.
这篇关于为PyPy的JIT编写快速代码的指南的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!