Why not always use psyco for Python code?

前端 未结 8 543
北恋
北恋 2020-12-13 06:29

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

Therefore, one has to wonder. Assuming you\'re always on a x8

相关标签:
8条回答
  • 2020-12-13 06:57

    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:

    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):

    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 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) Finally, there are some relatively obscure situations where using Psyco will actually introduce bugs. Some of them are listed here.

    0 讨论(0)
  • 2020-12-13 06:58

    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.

    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.

    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.

    0 讨论(0)
提交回复
热议问题