[Python-Dev] python optimization (original) (raw)

Raymond Hettinger raymond.hettinger at verizon.net
Fri Sep 16 06:26:35 CEST 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 LOADCONST 1 (2) 3 LOADCONST 2 (3) 6 BINARYADD 7 RETURNVALUE 8 LOADCONST 0 (None) 11 RETURNVALUE

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-Dev mailing list