为 PyPy 的 JIT 编写快速代码的指南 [英] Guidelines to write fast code for PyPy's JIT

查看:49
本文介绍了为 PyPy 的 JIT 编写快速代码的指南的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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. 将脚本打包成函数
  2. 显式删除变量
  3. 给出或暗示变量类型的可能方式
  4. 以某种方式编写循环

推荐答案

PyPy wiki's at 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"或类似的语句可能很有用,但我不记得我在哪里看到的.我也相信我已经看到了一些关于重构conditional-paths-in-loops的建议(这似乎是过时了.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆