[Python-Dev] python optimization

Raymond Hettinger raymond.hettinger at verizon.net
Fri Sep 16 00:26:35 EDT 2005


[Neal Becker] 
> >>I don't know to what extent these kind of optimizations are
available to
> >>cpython.  For example, are constant calculations removed from loops?

[Brett Cannon]
> > If you mean ``2+3``, then yes.

[Greg Ewing] 
> Actually, no. Constant folding *could* be done, but it currently
isn't:
> 
>  >>> def f():
> ...   return 2+3
> ...
>  >>> import dis
>  >>> dis.dis(f)
>    2           0 LOAD_CONST               1 (2)
>                3 LOAD_CONST               2 (3)
>                6 BINARY_ADD
>                7 RETURN_VALUE
>                8 LOAD_CONST               0 (None)
>               11 RETURN_VALUE


That looks like a disassembly from the ancient and primitive Py2.3 ;-)
It looks a little different in the ahead-of-its-time Py2.5 alpha:





Python 2.5a0 (#46, Sep 15 2005, 00:51:34) [MSC v.1200 32 bit (Intel)] on
win32
Type "copyright", "credits" or "license()" for more information.

>>> def f():
	return 2+3

>>> import dis
>>> dis.dis(f)
  2           0 LOAD_CONST               3 (5)
              3 RETURN_VALUE   




Raymond




More information about the Python-list mailing list