为什么不总是将psyco用于Python代码

为什么不总是将psyco用于Python代码

本文介绍了为什么不总是将psyco用于Python代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

psyco 似乎在优化Python代码方面非常有帮助,并且它以非介入方式进行道路.

psyco seems to be quite helpful in optimizing Python code, and it does it in a very non-intrusive way.

因此,人们不得不怀疑.假设您始终使用x86架构(这是当今大多数应用程序在其中运行的架构),为什么不对所有Python代码始终使用psyco呢?它有时会犯错误并破坏程序的正确性吗?在某些奇怪的情况下会增加运行时间吗?

Therefore, one has to wonder. Assuming you're always on a x86 architecture (which is where most apps run these days), why not just always use psyco for all Python code? Does it make mistakes sometimes and ruins the correctness of the program? Increases the runtime for some weird cases?

您对此有否消极经验?到目前为止,我最不利的经验是它使我的代码速度仅提高了15%.通常情况会更好.

Have you had any negative experiences with it? My most negative experience so far was that it made my code faster by only 15%. Usually it's better.

自然地,使用psyco并不能代替有效的算法和编码.但是,如果您能以两行(导入和调用psyco)为代价提高代码的性能,我就没有理由不这样做.

Naturally, using psyco is not a replacement for efficient algorithms and coding. But if you can improve the performance of your code for the cost of two lines (importing and calling psyco), I see no good reason not to.

推荐答案

1)内存开销是主要开销,如其他答案所述.您还需要支付编译费用,如果您没有选择的话,这可能会让人望而却步.从用户参考:

1) The memory overhead is the main one, as described in other answers. You also pay the compilation cost, which can be prohibitive if you aren't selective. From the user reference:

2)Psyco编译实际上可能会损害性能.再次从用户指南(已知错误" 部分):

2) Performance can actually be harmed by Psyco compilation. Again from the user guide ("known bugs" section):

  • 必须避免使用内置的mapfilter函数,并应将其替换为列表理解.例如,应将map(lambda x: x*x, lst)替换为可读性更强的最新语法[x*x for x in lst].
  • 正则表达式的编译似乎没有从Psyco中受益. (由于它是C代码,因此不影响正则表达式的执行.)请勿在此模块上启用Psyco;否则,请参见图9.如有必要,请明确将其禁用,例如通过调用psyco.cannotcompile(re.compile).
  • The built-in map and filter 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实际上会引入错误.其中一些在此处列出.

3) Finally, there are some relatively obscure situations where using Psyco will actually introduce bugs. Some of them are listed here.

这篇关于为什么不总是将psyco用于Python代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 18:03