[Image-SIG] Comparison of PIL and GD speed for setting pixels through python

Douglas Bagnall douglas at paradise.net.nz
Tue Feb 6 00:59:48 CET 2007

John Barratt wrote:

> I think that has about exhausted the possible combinations to test, but 
> if anyone has any other suggestions, please let me know.  Also if anyone 
> has any ideas as to why the PIL/raw/c version should go so much faster 
> than the gd/raw/c version I would be interested to know, as the core 
> looping code is basically identical...

um.. cache locality?  With GD you have

  for (u=0; u<x; u++) {
    for (v=0; v<y; v++) {
      d[v][u] = colour;

which goes through every row for each column, jumping back and forth in
memory.  You need to switch the order of the loops, like you have in the
PIL example:

  for (u=0; u<xSize; u++) {
    for (v=0; v<xSize; v++) {
      data[u][v] = colour;

...which, BTW, as written, would not be so successful on non-square images.

However, this shows how far the benchmark has got from real world usage.
 As Chris Barker pointed out, if you are doing any kind of work to find
your pixels, these effects would become negligible.

Have you looked at PyGame? it seems to have drawing commands too.

Douglas Bagnall

More information about the Image-SIG mailing list