Performance question about math operations

Jonathan Hogg jonathan at onegoodidea.com
Tue Jul 2 03:41:26 EDT 2002


On 2/7/2002 7:00, in article mailman.1025589663.1583.python-list at python.org,
"Andrew P. Lentvorski" <bsder at mail.allcaps.org> wrote:

> 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:

Could you post the actual code, or is it too long/proprietary? How are all
the magic numbers arrived at? Does it really look as simple as you
suggested?

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

Try disassembling the loop:

>>> def foo( n ):
...     i = 0
...     while i < 100000:
...         i = i + 1
... 
>>> import dis
>>> dis.dis( foo )
          0 SET_LINENO               1

          3 SET_LINENO               2
          6 LOAD_CONST               1 (0)
          9 STORE_FAST               1 (i)
          [...]

You could look out for LOAD_GLOBAL instructions which are expensive. Another
thread here recently discussed why and a couple of methods of speeding them
up.

If it turns out that your loop is as fast as it's going to get, your best
bet is to convert it to C instead. You can do this by writing an extension
module. There's some documentation on the Python website that should get you
started on writing extension modules.

Jonathan




More information about the Python-list mailing list