Some patterns for fast Python. Know any others?
一些快速 Python 的模式。你知道其他的吗?
- Avoid overengineering datastructures. Tuples are better than objects (try namedtuple too though). Prefer simple fields over getter/setter functions.
- 避免过度设计数据结构。元组比对象更好(不过也可以试试 namedtuple)。优先使用简单字段而不是 getter/setter 函数。
- Built-in datatypes are your friends. Use more numbers, strings, tuples, lists, sets, dicts. Also check out the collections library, esp. deque.
- 内置数据类型是你的朋友。多使用数字、字符串、元组、列表、集合、字典。还可以查看 collections 库,特别是 deque。
- Be suspicious of function/method calls; creating a stack frame is expensive.
- 对函数/方法调用保持怀疑态度;创建一个堆栈帧是很昂贵的。
- Don't write Java (or C++, or Javascript, ...) in Python.
- 不要在 Python 中写 Java(或 C++、或 Javascript 等)。
- Are you sure it's too slow? Profile before optimizing!
- 你确定它太慢了吗?在优化之前先进行性能分析!
- The universal speed-up is rewriting small bits of code in C. Do this only when all else fails.
- 通用的加速方法是用 C 重写小部分代码。只有在其他方法都失败时才这样做。
一些快速 Python 的模式。你知道其他的吗?
- Avoid overengineering datastructures. Tuples are better than objects (try namedtuple too though). Prefer simple fields over getter/setter functions.
- 避免过度设计数据结构。元组比对象更好(不过也可以试试 namedtuple)。优先使用简单字段而不是 getter/setter 函数。
- Built-in datatypes are your friends. Use more numbers, strings, tuples, lists, sets, dicts. Also check out the collections library, esp. deque.
- 内置数据类型是你的朋友。多使用数字、字符串、元组、列表、集合、字典。还可以查看 collections 库,特别是 deque。
- Be suspicious of function/method calls; creating a stack frame is expensive.
- 对函数/方法调用保持怀疑态度;创建一个堆栈帧是很昂贵的。
- Don't write Java (or C++, or Javascript, ...) in Python.
- 不要在 Python 中写 Java(或 C++、或 Javascript 等)。
- Are you sure it's too slow? Profile before optimizing!
- 你确定它太慢了吗?在优化之前先进行性能分析!
- The universal speed-up is rewriting small bits of code in C. Do this only when all else fails.
- 通用的加速方法是用 C 重写小部分代码。只有在其他方法都失败时才这样做。
View 80 previous comments
查看之前的 80 条评论
查看之前的 80 条评论
- Avoid attribute lookups by caching in a local, especially if it's occurring in a high-iteration loop.
通过在本地缓存来避免属性查找,特别是在高迭代循环中。Sep 11, 2012 2012 年 9 月 11 日 - +Ralph Corderoy that's a good example of "moving loop invariants out of the loop". Everything that doesn't change can be cached as local variables, even instance methods.
+Ralph Corderoy 这是一个“将循环不变量移出循环”的好例子。所有不变的东西都可以缓存为局部变量,甚至实例方法。
This is one of the optimizations that the pypy JIT does for you.
这是 pypy JIT 为你做的优化之一。
Worth noting that local variable lookup is faster than global variable or builtin lookup, and much faster than attribute lookup. One micro-optimization is to move everything (even builtins) into local variables. It tends to make your code less readable, so only do it if the performance benefit is really worth it.
值得注意的是,局部变量查找比全局变量或内置变量查找更快,比属性查找快得多。一个微优化是将所有东西(甚至内置变量)移到局部变量中。这往往会使你的代码可读性降低,所以只有在性能收益确实值得时才这样做。Sep 11, 2012 2012 年 9 月 11 日 - I Think "avoid function is very difcult matter.
我认为“避免函数”是非常困难的事情。Sep 11, 2012 2012 年 9 月 11 日 - +Michael Foord, yes, loop-invariant hoisting. Attribute lookup can be particularly painful. The dis module dis() can be fun to see what's going on. Really, locals aren't `looked up', a local is given a slot at compile-time and its index is used from then on.
+Michael Foord,是的,循环不变量提升。属性查找可能特别痛苦。dis 模块 dis() 可以看看发生了什么。实际上,局部变量不是“查找”的,局部变量在编译时被赋予一个槽位,从那时起就使用它的索引。Sep 11, 2012 2012 年 9 月 11 日 - Universal speed-up is Pypy: speed.pypy.org
通用加速器是 Pypy: speed.pypy.orgSep 11, 2012 2012 年 9 月 11 日 - Time to close comments. 是时候关闭评论了。Sep 11, 2012 2012 年 9 月 11 日
Commenting is disabled for this post.
此帖子的评论已被禁用。
此帖子的评论已被禁用。