Avian’s Blog

Electronics and Free Software

Another one from the Python dept

23.04.2008 19:32

Update: Please ignore this rant. I don't know what I've been doing wrong here, but my later measurements confirm Hruške's findings.

Python provides a convenient in operator that returns true if the left hand value is an element of the right hand list or dictionary. That's nice and all, but the performance makes it pretty useless in all but trivial cases. Take a look at this graph:

On the vertical axis is time needed for a loop to finish while test_size value is on the horizontal axis.

I ran these three loops that are exactly equivalent as far as the effect of the if statement is concerned:

"in list": the obvious way

list = range(test_size)

for m in xrange(2 * test_size):
	if m in list:
		a += 1
	else:
		b += 1

"in dict": converting to hash doesn't help

list = range(test_size)
dict = dict.fromkeys(list, 1)

for m in xrange(2 * test_size):
	if m in dict:
		a += 1
	else:
		b += 1

"dict.get": the right way

list = range(test_size)
dict = dict.fromkeys(list, 1)

for m in xrange(2 * test_size):
	r = dict.get(m, None)
	if r:
		a += 1
	else:
		b += 1
Posted by Tomaž | Categories: Code
Comments
Add a new comment

Your name

Your email (optional)

Your web site (optional)


(No HTML tags allowed. Separate paragraphs with a blank line.)