No swap function in Python?

Skip Montanaro skip at pobox.com
Wed May 30 23:18:39 EDT 2001


    [ snipped example of eliding BUILD_TUPLE/UNPACK_TUPLE ]

    Nick> ..that looks a lot faster than the 'normal' way!  (maybe there
    Nick> should be a built-in operator or something to do exactly that)

    Nick> ..what is a peephole optimizer? and where can I get one?

You might poke around looking for Michael Hudson's (I think he's the author)
byte code hacks package.  The freeze/xfreeze family also uses some peephole
optimization techniques.  I wrote a demonstration peephole optimizer which I
mentioned in a post earlier today.  If you're anxious to dive into it (it's
nearly three years old and was written for Python 1.5.1), I just remembered
it's available from

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

(search for "peephole").  An HTML version of the paper I presented at SPAM-7
is also there.

A peephole optimizer is generally a last-ditch effort at reorganizing code
(often, the assembly code generated by an earlier pass of the compiler) by
only considering patterns that extend over a relatively small string of
opcodes.  If you think of watching a construction site through a knothole in
a fence, you'll get the idea.  You only see a little bit of the whole
program at once.  In the absence of other optimization techniques you can
sometimes get significant speedups on fairly specific bits of code, but
peephole optimization techniques tend to generally yield only modest
benefits.

-- 
Skip Montanaro (skip at pobox.com)
(847)971-7098




More information about the Python-list mailing list