Does CPython already has Peephole optimizations?

Steven D'Aprano steve+comp.lang.python at pearwood.info
Mon Feb 17 09:59:46 CET 2014


On Mon, 17 Feb 2014 13:54:25 +0530, Laxmikant Chitare wrote:

> 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 check these things for yourself:

import dis
dis.dis(function)


will show you the byte code.

But in general, I would expect not. CPython (that's the Python you 
probably use) doesn't do a lot of optimization apart from some simple 
constant folding. If you're interested in optimizing Python, you should 
look at the JIT optimizing Python compiler, PyPy.


-- 
Steven



More information about the Python-list mailing list