Code speedup tips

Carl Banks imbosol-1046792639 at aerojockey.com
Tue Mar 4 11:15:25 EST 2003


Sean Richards wrote:
> On Sun, 2 Mar 2003 09:07:18 -0500, Justin Shaw wrote:
> 
>> What are you using to draw the results?  I have coded up some of
>> Wolfram's stuff and had a bottle neck in the drawing of the pictures.
> 
> Hi,
> 
> I am using a Tkinter PhotoImage to draw the CA. I ran the python
> profiler over the new improved code and it uses ~25% of the processing
> time to draw the picture. So you are right there is a bottle neck in the
> drawing of the picture. Did you come up with a quicker way of displaying
> the array as an image?


Again, setting an image pixel-by-pixel is almost always the wrong way
to do it.

I don't know anything about this PhotoImage thing, but I warrant
there's a way to set every pixel at once, maybe using data passed in a
string.  Find it, then use Numeric operations to get the data in the
required format, then use the Numeric tostring method to extract the
data into a string, and finally pass in the string.

To exemplify, here is how I would convert Numeric data into an image
using Python Imaging Library:

    img = Image.fromstring('L',(width,height),
                           ((1-lattice)*255).astype(Int8).tostring())


(One other thing: Python has added a buffer protocol, to be supported
by objects that store raw data.  If Numeric and PhotoImage both
support it, you might not need the tostring call.  In fact, the
example above didn't require it on my system.)


-- 
CARL BANKS




More information about the Python-list mailing list