Python Patterns - An Optimization Anecdote

The above link is to an article by Guido van Rossum on the Python website. Needless to say, anything Guido says on the topic of Python is worth looking at (he is the author of the Python programming language).
I thought of this particular article while reading a post, trying to decide how to check if an object is a sequence. While all the contributors to the discussion are helpful, none actually checks the performance of the proposed solutions. This is typical of the posts you see on various forums.
Guido's article highlights how straightforward it is to do basic testing most of the time. Here is a quick summary of performance of the proposed solutions in the above link:import time
# Guido's timing function
def timing(f, n, a):
print f.__name__,
r = range(n)
t1 = time.clock()
for i in r:
f(a); f(a); f(a); f(a); f(a); f(a); f(a); f(a); f(a); f(a)
t2 = time.clock()
print round(t2-t1, 3)
if __name__ == "__main__":
#For Example
# some functions to check if an object is a sequence
def isit(obj):
try:
it = iter(obj)
return True
except TypeError:
return False
isit2 = lambda obj: isinstance(obj,basestring) or \ getattr(obj,'__iter__',False)
def isit3(obj):
return (isinstance(obj,basestring) or getattr(obj,'__iter__',False))
#...then:
'''
>>> timing(isit3, 100000, [])
isit3 0.99
>>> timing(isit2, 100000, [])
0.99
>>> timing(isit, 100000, [])
isit 0.53
'''
