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:or, more elegantly:
value = dict[key]
else:
value = None
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: Chris Waters
| @ October 30, 2007 7:43:51 PM CST ( ) |
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: Chris Waters
| @ October 30, 2007 7:28:12 PM CST ( ) |
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: Chris Waters
| @ September 17, 2007 4:17:27 PM CDT ( ) |
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: Chris Waters
| @ February 17, 2006 12:34:16 PM CST ( ) |
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: Chris Waters
| @ February 16, 2006 2:28:36 PM CST ( ) |
|
|

