Tuesday, February 5, 2008

Optimizing your Python Program: Code to watch out for

Change a list to a set


My all-time favorite easy optimization is replacing a list where I'm checking for duplicates with a set. The savings are enormous - in one instance taking a method's execution time from 650 seconds to 71 seconds - an order of magnitude! The key is, however, that your code is checking for duplicates in the list - like this:


if strIp not in myElems.ipAddrs:
myElems.ipAddrs.append(strIp)


To make this change, just change your list initialization from


myElems = []
## or
myElems = [initialElement]


to


myElems = set([])
## or
myElems = set([initialElement])


And change the way you add to the list - you don't need to check for duplicates any more:

myElems.ipAddrs.add(strIp)

Labels:

Thursday, January 31, 2008

Favorite Python Optimizations: Slots

If you are using any objects in Python, here is a quick way to knock a couple of seconds off the execution time - add slots. This is maybe the easiest optimization mod ever. It doesn't produce massive gains, but every little bit helps. So for a basic object like

class MyClass:
def __init__(self, var1, var2, var3):
self.myVar1 = var1
self.myVar2 = var2
self.myVar3 = var3


you add one line creating a list of slots like this:

class MyClass:
__slots__ = ['myVar1', 'myVar2', 'myVar3']
def __init__(self, var1, var2, var3):
self.myVar1 = var1
self.myVar2 = var2
self.myVar3 = var3

and you get a bit of a speed up!

Labels: