Does CPython already has Peephole optimizations?

Peter Otten __peter__ at web.de
Mon Feb 17 09:45:47 CET 2014


Laxmikant Chitare wrote:

> Hello All,
> 
> I read about this article:
> 
http://www.python.org/workshops/1998-11/proceedings/papers/montanaro/montanaro.html
> 
> Just wanted to clarify whether CPython already includes these kind of byte
> code optimizations? Are all the temporary variables removed when byte code
> is generated?

You can find out for yourself:

Python 3.4.0rc1+ (default:2ba583191550+, Feb 12 2014, 00:08:44) 
[GCC 4.6.1] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import dis
>>> def f():
...     a, b, c = 1, 2, 3
... 
>>> dis.dis(f)
  2           0 LOAD_CONST               4 ((1, 2, 3))
              3 UNPACK_SEQUENCE          3
              6 STORE_FAST               0 (a)
              9 STORE_FAST               1 (b)
             12 STORE_FAST               2 (c)
             15 LOAD_CONST               0 (None)
             18 RETURN_VALUE
>>> def g():
...     q = 2 + 3j
... 
>>> dis.dis(g)
  2           0 LOAD_CONST               3 ((2+3j))
              3 STORE_FAST               0 (q)
              6 LOAD_CONST               0 (None)
              9 RETURN_VALUE

If you can read C there is also

http://hg.python.org/cpython/file/180e4b678003/Python/peephole.c




More information about the Python-list mailing list