Speeding up Simple Canvas Loop

Randall Hopper aa8vb at vislab.epa.gov
Mon May 10 10:35:25 EDT 1999


Guido van Rossum:
 |Randall Hopper writes:
 |
 |>     create_line = canvas.create_line
 |>     for line in lines:
 |>       create_line( line, width=0 )
 |> 
 |> Can canvas items for Tkinter canvases be created by a C extension (these
 |> vertices are actually coming from C code)?
 |> 
 |> The performance of this GUI app is acceptable except for the above loop,
 |> and it alone can literally take minutes (400-30k iterations).
 |
 |If you look carefully in the Tkinter.py source at how much Python code
 |gets executed here you'll notice that it is a lot! 
...
 |I remember fixing a similar situation by bypassing most of that
 |Python code and calling the underlying Tcl/Tk command directly using
 |the canvas.tk.call() method.

Thanks for the tip.  By breaking out what Tkinter is doing, I found that an
amazing 56% to 64% of the time spent in this loop is doing a _flatten on
the lists:

    lines = map( _flatten, lines )

    for line in lines:
      apply( self.canvas.tk.call,
             ( self.canvas._w, 'create', 'line' ) + line + ('-width', 0) )

Sample datasets had 4,000 to 29,000 lines with 174,000 to 360,000 points.

Now if I can just figure out how to return a variable-length list of
doubles from C, I can knock off 30-50% of the overhead time not included in
this loop (currently using SWIG and fetching the vertices one-at-a-time in
a map() loop).

Randall




More information about the Python-list mailing list