Optimized bytecode in exec statement
steven at REMOVE.THIS.cybersource.com.au
Tue Feb 2 04:35:47 CET 2010
On Mon, 01 Feb 2010 22:19:43 -0300, Gabriel Genellina wrote:
> En Mon, 01 Feb 2010 20:05:16 -0300, Hermann Lauer
> <Hermann.Lauer at iwr.uni-heidelberg.de> escribió:
>> while trying to optimize some unpack operations with self compiling
>> code I wondered howto invoke the optimization in the exec statement.
>> Even starting with -O or -OO yields in Python 2.5.2 the same dis.dis
>> code, which
>> is appended below.
> There is not much difference with -O or -OO; assert statements are
> removed, __debug__ is False, docstrings are not stored (in -OO). Code
> generation is basically the same.
Also code of the form:
is compiled away if __debug__ is false.
> In general, x+0 may execute arbitrary code, depending on the type of x.
> Only if you could determine that x will always be an integer, you could
> optimize +0 away. Python (the current version of CPython) does not
> perform such analysis; you may investigate psyco, Unladen Swallow,
> ShedSkin for such things.
Keep in mind that the peephole optimizer in CPython does constant
folding: 1+1 compiles to 2. Older versions of Python, and other
implementations, may not.
But as a general rule, Python does very little optimization at compile
time. To a first approximation, "remove asserts" is all that it does.
More information about the Python-list