VisLab

Chris Waters' Weblog

Visualizing the Vislab

I did a little more 'thorough' testing on the differences between dict.get(key) and dict[key]. I tested for both keys that do and do not exist, and I seem to have different results than what I did last time =( 
 
It seems dict[key] is about twice as fast as dict.get(key) when the key does exist in the dict. On the other hand, dict[key] + exception handling is twice as slow as dict.get(key) when key is not in dict. Here's the extra twist: checking if the key is in dict and then getting the appropriate value is only slightly slower than simply table[key]. This is something like: 

if key in dict: 
value = dict[key] 
else: 
value = None
or, more elegantly: 
value = None if key not in dict else dict[key]
 
This method is fractionally slower than dict[key] when key is in the table, but it's considerably faster when key is not in the table. Also, this method is consistantly faster than dict.get(key) in both cases. 
 
The script can be found [here
The output for 100,000 iterations of each case can be found [here
 
The output shows the name of each test case followed by the commands executed in the test. Total runtime for each follow each. 
 
I may have done the cases a little naively, but I still think it shows some powerful differences. 

Posted by:

Installing GLEW 
Download the GLEW source from [here
Run the following command in the glew folder: 

> GLEW_DEST=/usr/local sudo make install
 
Installing GLEWpy 
Download the GLEWpy source from [here
Replace the setup.py in the glewpy folder with the modified setup.py from [here
Run the following command in the glewpy folder: 
> sudo python setup.py install
 

Posted by:

def GetWXBitmap(myImage): 
image = apply(wx.EmptyImage, myImage.size) 
image.SetData(myImage.convert("RGB").tostring()) 
# if the image has an alpha channel, 
# you can set it with this line: 
myImage = myImage.convert("RGBA") 
image.SetAlphaData(myImage.tostring()[3::4] ) 
return image.ConvertToBitmap()
 
FROM: 
http://wiki.wxpython.org/WorkingWithImages#head-7aa43a4a1e066fd28640ce86066ba0617afe2a8b 
 
Using wx.StaticBitmap (widget on the window to show the bitmap): 
http://wiki.wxpython.org/wxStaticBitmap?highlight=%28Bitmap%29 

Posted by:

It's interesting how one 'small' change can affect speed so much. During this morning's internet downtime, I was poking through some Moire drawing code looking for places to possibly optimize performance. This led me to write a quick performance testing script with hotshot that tests the runtime differences between dict.get(key) and dict[key]. 
 
It turns out that the dict.get() method is exponentially (haha *poke* at a comment someone made) faster than the indexing (?) method. I'm assuming this is due to the fact that the latter raises a key error if the key is not found. This may actually be something worth keeping in mind from now on. 
 
By the way, The Python Challenge has me in its grasp again. 

Posted by:

I've been skimming through the Python Coroutine PEP [link], and it's making my brain hurt. I'm loving the way this is going to work, but the first example (thumbnail generator) just blows my mind. 
 
Things like this make me want to get more into reading these Python devs' blogs. 

Posted by:

Login Information
Username:
Password:

© Chris Waters. The views and opinions expressed in this page are strictly those of the page author. The contents of this page have not been reviewed or approved by Mississippi State University.