为什么不总是对 Python 代码使用 psyco?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/575385/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why not always use psyco for Python code?
提问by Eli Bendersky
psycoseems to be quite helpful in optimizing Python code, and it does it in a very non-intrusive way.
psyco似乎对优化 Python 代码很有帮助,而且它以一种非常非侵入性的方式进行。
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?
因此,不得不怀疑。假设你总是在 x86 架构上(这是当今大多数应用程序运行的地方),为什么不总是psyco
用于所有 Python 代码?是否有时会出错,破坏程序的正确性?增加一些奇怪情况的运行时间?
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.
你有过什么负面的经历吗?到目前为止,我最负面的体验是它使我的代码速度仅提高了 15%。通常会更好。
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.
自然,使用 psyco 并不能替代高效的算法和编码。但是,如果您能以两行代码(导入和调用 psyco)为代价来提高代码的性能,我认为没有充分的理由不这样做。
采纳答案by ire_and_curses
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:
1)内存开销是主要的,如其他答案中所述。您还需要支付编译成本,如果您没有选择性,这可能会令人望而却步。来自用户参考:
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) Performance can actually be harmed by Psyco compilation. Again from the user guide ("known bugs"section):
2) Psyco 编译实际上会损害性能。再次来自用户指南(“已知错误”部分):
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)
.
还有性能错误:Psyco 减慢代码而不是加速代码的情况。很难完整列出可能的原因,但这里有一些常见的原因:
- 必须避免使用内置函数
map
和filter
函数,并用列表理解替换。例如,map(lambda x: x*x, lst)
应该替换为更易读但更新的语法[x*x for x in lst]
。- 正则表达式的编译似乎并没有从 Psyco 中受益。(正则表达式的执行不受影响,因为它是 C 代码。)不要在这个模块上启用 Psyco;如有必要,明确禁用它,例如通过调用
psyco.cannotcompile(re.compile)
.
3) Finally, there are some relatively obscure situations where using Psyco will actually introduce bugs. Some of them are listed here.
3) 最后,在一些相对模糊的情况下,使用 Psyco 实际上会引入 bug。其中一些列在此处。
回答by HUAGHAGUAH
Psyco currently uses a lot of memory. It only runs on Intel 386-compatible processors (under any OS) right now. There are some subtle semantic differences (i.e. bugs) with the way Python works; they should not be apparent in most programs.
Psyco 当前使用大量内存。它现在只能在 Intel 386 兼容处理器(在任何操作系统下)上运行。Python 的工作方式存在一些细微的语义差异(即错误);它们在大多数程序中不应该很明显。
See also the caveats section. For a hard example, I noticed that my web app with Cheetah-generated templates and DB I/O gained no appreciable speedup.
另请参阅注意事项部分。举一个很难的例子,我注意到我的带有 Cheetah 生成模板和 DB I/O 的 Web 应用程序没有获得明显的加速。
回答by Devin Jeanpierre
When using pyglet I found that I couldn't use psyco on the entire app without making my app non-functional. I could use it in small sections of math-heavy code, of course, but it wasn't necessary, so I didn't bother.
使用 pyglet 时,我发现我无法在整个应用程序上使用 psyco,而不会使我的应用程序无法运行。当然,我可以在需要大量数学的代码的一小部分中使用它,但这不是必需的,所以我没有打扰。
Also, psyco has done strange things with my profiling results (such as, well, not alter them at all from the non-psyco version). I suspect it doesn't play nice with the profiling code.
此外,psyco 对我的分析结果做了一些奇怪的事情(例如,完全没有从非 psyco 版本更改它们)。我怀疑它不适合分析代码。
I just don't really use it unless I really want the speed, which is not all that often. My priority is algorithm optimization, which generally results in nicer speedups.
我只是不真正使用它,除非我真的想要速度,这并不常见。我的首要任务是算法优化,这通常会带来更好的加速。
回答by MrTopf
It also depends where your bottleneck is. I am mostly doing web apps and there the bottlenecks are probably more IO and database. So you should know where to optimize.
这也取决于你的瓶颈在哪里。我主要做网络应用程序,瓶颈可能是更多的 IO 和数据库。所以你应该知道在哪里优化。
Also beware that maybe you first should think about your code instead of directly throwing psyco at it. So I agree with Devin, that algorithm optimizations should come first and they might have a smaller chance of unwanted sideeffects.
还要注意,也许您首先应该考虑您的代码,而不是直接向其抛出 psyco。所以我同意 Devin 的观点,算法优化应该排在第一位,它们可能产生不必要的副作用的可能性较小。
回答by TNTDoctor
psyco is dead and not longer maintained. It is time to find another
psyco 已死,不再维护。是时候寻找另一个
回答by Andrew Szeto
One should never rely on some magic bullet to fix your problems. Using psyco to make a slow program faster is usually not necessary. Bad algorithms can be rewritten, and parts that requirespeed could be written in another language. Of course, your question asks why we don't use it for the speed boost anyways, and there's a bit of overhead when you use psyco. Psyco uses memory, and those two lines just sorta feellike overhead when you look at them. As for my personal reason on why I don't use psyco, it's because it doesn't support x86_64, which I see as the new up and coming architecture (especially with 2038 approaching sooner or later). My alternative is pypy, but I'm not entirely fond of that either.
永远不应该依靠某种灵丹妙药来解决您的问题。通常没有必要使用 psyco 使慢程序更快。糟糕的算法可以重写,需要速度的部分可以用另一种语言编写。当然,你的问题是问我们为什么不使用它来提高速度,而且当你使用 psyco 时会有一些开销。Psyco的使用内存,并且这两条线只是八九不离十感觉就像开销,当你看他们。至于我为什么不使用 psyco 的个人原因,那是因为它不支持 x86_64,我将其视为新兴的架构(尤其是 2038 迟早会到来)。我的选择是 pypy,但我也不完全喜欢它。
回答by Jason Baker
A couple of other things:
其他几件事:
- It doesn't seem to be very actively maintained.
- It can be a memory hog.
- 它似乎没有得到非常积极的维护。
- 它可能是一个记忆猪。
回答by Ali Afshar
Quite simply: "Because the code already runs fast enough".
很简单:“因为代码已经运行得足够快了”。