[pygtk] help with gtk.gdk performance

Gustavo J. A. M. Carneiro gjc at inescporto.pt
Thu Aug 5 04:52:12 WST 2004

A Qua, 2004-08-04 às 19:01, Robert Gibbs escreveu:
> I have an application that makes about 500 calls per
> second to an off-screen gdk.Pixmap.  At the end of the
> 500 calls it triggers a single expose event that
> causes the Pixmap to be drawn to the window in the
> usual way. It runs at about 6% CPU.
> I suspected the bottleneck was in the communication
> with the X Server so I re-wrote it in 'c' to see if
> the performance was the same.
> It turned out that the 'c' application used only 0.3%
> CPU in doing the same thing.
> I plan to scale the application to do about ten times
> the amount of drawing that it does now and I want to
> keep the application in Python.
> Can anyone give me an idea on how to improve the
> performance? 
> Why should calling something like
> gtk.gdk.pixmap.draw_point() from Python 500 times take
>  almost 20 times greater CPU usage than making the
> same 500 calls from a 'c' application? Once the shared
> libraries are loaded, the per call overhead should be
> about the same right?

  No. Python calls have an added overhead relative to pure C code.
Python code being 20 times slower than C sounds just about right :-)

  I think the only way to overcome your performance problems is to write
a small C module to do the CPU intensive part, and control this C module
from the main python application.  You need to isolate a piece of code
	1. is very simple
	2. gets executed _many_ times (python profiler will help determine

Then, take that code, write it in C, and interface it with python.


Gustavo J. A. M. Carneiro
<gjc at inescporto.pt> <gustavo at users.sourceforge.net>
The universe is always one step beyond logic

More information about the pygtk mailing list