I've got a in if-elif-elif-else statement in which 99% of the time, the else statement is executed:
if something == 'this':
doThis()
elif something == 'that':
doThat()
elif something == 'there':
doThere()
else:
doThisMostOfTheTime()
This construct is done a lot, but since it goes over every condition before it hits the else I have the feeling this is not very efficient, let alone Pythonic. On the other hand, it does need to know if any of those conditions are met, so it should test it anyway.
Does anybody know if and how this could be done more efficiently or is this simply the best possible way to do it?
Best Answer
The code...
...looks like it ought to be faster, but it's actually slower than the
if
...elif
...else
construct, because it has to call a function, which can be a significant performance overhead in a tight loop.Consider these examples...
1.py
2.py
3.py
4.py
...and note the amount of CPU time they use...
...using the user time from
time(1)
.Option #4 does have the additional memory overhead of adding a new item for every distinct key miss, so if you're expecting an unbounded number of distinct key misses, I'd go with option #3, which is still a significant improvement on the original construct.