Does Python optimize low-power functions?

Michael Torrie torriem at gmail.com
Sat Dec 7 21:00:09 EST 2013


On 12/06/2013 12:32 PM, Nick Cash wrote:
> Nope:
> 
> Python 3.3.0 (default, Sep 25 2013, 19:28:08) 
> [GCC 4.7.2] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
>>>> import dis
>>>> dis.dis(lambda x: x*x)
>   1           0 LOAD_FAST                0 (x) 
>               3 LOAD_FAST                0 (x) 
>               6 BINARY_MULTIPLY      
>               7 RETURN_VALUE         
>>>> dis.dis(lambda x: x**2)
>   1           0 LOAD_FAST                0 (x) 
>               3 LOAD_CONST               1 (2) 
>               6 BINARY_POWER         
>               7 RETURN_VALUE         
> 
> 
> The reasons why have already been answered, I just wanted to point
> out that Python makes it extremely easy to check these sorts of
> things for yourself.

But this is just the interpreter bytecode that dis is showing.  It's not
showing the underlying implementation of binary_power, for example.
That could be defined in C code with any number of optimizations, and
indeed it appears that some are being done.  dis is great for showing
how python code breaks down, but it can't tell you much about the code
that underlies the byte codes themselves.



More information about the Python-list mailing list