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 重写小部分代码。只有在其他方法都失败时才这样做。
Shared publiclyView activity
公开分享 • 查看活动