psyco似乎在优化 Python 代码方面很有帮助,而且它以一种非常非侵入性的方式进行。
因此,人们不得不怀疑。假设你总是在 x86 架构上(现在大多数应用程序都在这个架构上运行),为什么不总是对所有 Python 代码使用 psyco
呢?它有时会出错并破坏程序的正确性吗?增加一些奇怪情况的运行时间?
你有过负面的经历吗?到目前为止,我最消极的经历是它使我的代码速度仅提高了 15%。通常会更好。
当然,使用 psyco 并不能替代高效的算法和编码。但是,如果您可以以两行代码(导入和调用 psyco)的代价提高代码的性能,我认为没有什么不这样做的好理由。
最佳答案
1) 内存开销是主要开销,如其他答案中所述。您还需要支付编译成本,如果您没有选择性,这可能会令人望而却步。来自 user reference :
Compiling everything is often overkill for medium- or large-sized applications. The drawbacks of compiling too much are in the time spent compiling, plus the amount of memory that this process consumes. It is a subtle balance to keep.
2) Psyco 编译实际上会损害性能。再次来自用户指南("known bugs" 部分):
There are also performance bugs: situations in which Psyco slows down the code instead of accelerating it. It is difficult to make a complete list of the possible reasons, but here are a few common ones:
- The built-in
map
andfilter
functions must be avoided and replaced by list comprehension. For example,map(lambda x: x*x, lst)
should be replaced by the more readable but more recent syntax[x*x for x in lst]
.- The compilation of regular expressions doesn't seem to benefit from Psyco. (The execution of regular expressions is unaffected, since it is C code.) Don't enable Psyco on this module; if necessary, disable it explicitely, e.g. by calling
psyco.cannotcompile(re.compile)
.
3) 最后,在一些相对模糊的情况下,使用 Psyco 实际上会引入错误。其中一些是listed here .
关于python - 为什么不总是将 psyco 用于 Python 代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/575385/