The docs only say that Python interpreter performs “basic optimizations”, without going into any detail. Obviously, it’s implementation dependent, but is there any way to get a feel for what type of things could be optimized, and how much run-time savings it could generate?
Is there any downside to using -O?
The only thing I know is that -O disables assert, but presumably one shouldn’t use assert for things that could still go wrong in production.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
In Python 2.7, -O has the following effect:
- the byte code extension changes to
.pyo - sys.flags.optimize gets set to 1
__debug__is False- asserts don’t get executed
In addition -OO has the following effect:
- sys.flags.optimize gets set to 2
- doc strings are not available
To verify the effect for a different release of CPython, grep the source code for Py_OptimizeFlag.
Link to official documentation: https://docs.python.org/2.7/tutorial/modules.html#compiled-python-files
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0