What is the use of Python's basic optimizations mode? (python -O)

后端 未结 7 687
终归单人心
终归单人心 2020-12-25 10:24

Python has a flag -O that you can execute the interpreter with. The option will generate \"optimized\" bytecode (written to .pyo files), and given twice, it wil

相关标签:
7条回答
  • 2020-12-25 10:51

    But don't you need a ton of assert statements for this to make a difference? Do you have any code where this is worthwhile (and sane?)

    As an example, I have a piece of code that gets paths between nodes in a graph. I have an assert statement at the end of the function to check that the path doesn't contain duplicates:

    assert not any(a == b for a, b in zip(path, path[1:]))
    

    I like the peace of mind and clarity that this simple statement gives during development. In production, the code processes some big graphs and this single line can take up to 66% of the run time. Running with -O therefore gives a significant speed-up.

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