Python 2 times slower than Perl

Skip Montanaro skip at pobox.com
Tue Jul 17 18:17:09 EDT 2001


    Christopher> I can't speak for Perl, but Python definately does not
    Christopher> evaluate the expression at compile time.  This is a problem
    Christopher> I've had with Python.  I wish that when given the -O option
    Christopher> the compiler would do constant subexpression elimination.

It's rare in normal Python code to actually see a constant expression
computed inside a loop.  Fortunately, it would appear most folks aren't that
dumb, so while you can take steps to eliminate them, there aren't generally
going to be very many to eliminate.

Experiments have been performed in this area.

* I once wrote a peephole optimizer that would precompute expressions like
  2.5*2.5:

      http://musi-cal.mojam.com/~skip/python/

  The biggest boost I saw was on complex arithmetic because complex
  constants aren't stored as constants at all, but as arithmetic
  expressions.

* Python2C may also do some of this:

      http://www.mudlib.org/~rassilon/p2c/

* The bytecode hacks might as well (sorry no URL, check Parnassus)

* Some versions of freeze used to apply a number of peephole techniques

* Finally, Armin Rigo doing some very interesting things with compiling
  functions on-the-fly:

      http://mail.python.org/pipermail/python-dev/2001-June/015503.html

-- 
Skip Montanaro (skip at pobox.com)
http://www.mojam.com/
http://www.musi-cal.com/




More information about the Python-list mailing list