I recently wrote a quick and dirty BFS implementation, to find diamonds in a directed graph. The BFS loop looked like this:
while toVisit:
y = toVisit.pop()
if y in visited: return "Found diamond"
visited.add(y)
toVisit.extend(G[y])
(G
is the graph - a dictionary from node names to the lists of their neighbors)
Then comes the interesting part:
I thought that list.pop()
is probably too slow, so I ran a profiler to compare the speed of this implementation with deque.pop - and got a bit of an improvement. Then I compared it with y = toVisit[0]; toVisit = toVisit[1:]
, and to my surprise, the last implementation is actually the fastest one.
Does this make any sense?
Is there any performance reason to ever use list.pop()
instead of the apparently much faster two-liner?