Basic optimization of python.

Diez B. Roggisch deets at nospam.web.de
Wed Apr 9 08:15:19 EDT 2008


Hrvoje Niksic wrote:

> "Diez B. Roggisch" <deets at nospam.web.de> writes:
> 
>>> Eg:
>>> a = 1 + 2
>>> .vs.
>>> a = 3
>>> which one is more effective? Does the compiler calculate the result at
>>> compile time? How about constant spreading?
>>
>> Algebraic optimizations aren't done AFAIK
> 
> Just try it:
> 
> Python 2.5.1 (r251:54863, Oct  5 2007, 13:36:32)
> [GCC 4.1.3 20070929 (prerelease) (Ubuntu 4.1.2-16ubuntu2)] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
>>>> dis.dis(lambda: 10+5)
>   1           0 LOAD_CONST               2 (15)
>               3 RETURN_VALUE

I remember times when that hasn't been the case - thus my answer.

[GCC 3.4.6 (Ubuntu 3.4.6-6ubuntu2)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
Welcome to rlcompleter2 0.96
for nice experiences hit <tab> multiple times
>>> import dis
>>> dis.dis(lambda: 1+2)
  1           0 LOAD_CONST               1 (1)
              3 LOAD_CONST               2 (2)
              6 BINARY_ADD
              7 RETURN_VALUE


Python 2.4.4 (#2, Mar  7 2008, 04:45:43)
[GCC 4.1.3 20070929 (prerelease) (Ubuntu 4.1.2-16ubuntu2)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import dis
>>> dis.dis(lambda: 1+2)
  1           0 LOAD_CONST               1 (1)
              3 LOAD_CONST               2 (2)
              6 BINARY_ADD
              7 RETURN_VALUE
>>>                      


But great to know it is done now.

Diez



More information about the Python-list mailing list