[Python-Dev] Performance question about math operations

Andrew P. Lentvorski bsder@mail.allcaps.org
Mon, 1 Jul 2002 19:50:19 -0700 (PDT)


I have a VLSI layout editor written in Python.  At its core, it has to
redraw a lot of polygons.  This requires a lot of coordinate conversion
mathematics.  Essentially the following loop:

#! /usr/bin/env python

def main():
    i = 0

    while i < 1000000:
        i = i + 1

        (1-678)*3.589
        -((1-456)*3.589)

if __name__=="__main__":
    main()

Now, I understand that looping in Python has overhead.  It turns out that
the loop without the math operations takes about .5 seconds.  Fine.
However, each line of math operations adds .75 seconds to the total loop
time for a total run time of about 2 seconds.  This is with -O enabled
(even though it doesn't seem to have any effect).

This same loop in C++ (with classes, indirection, copy contruction, etc)
takes about .05 seconds.

That's about a factor of 30 (1.5 / .05) difference even if I cancel out
the loop overhead.  I could handle factor of 2 or 4, but 30 seems a bit
high.

What is eating all that time?  And can I do anything about it?

-a