efficiency question
David Harvey
dmharvey at math.harvard.edu
Fri Jun 30 06:51:01 EDT 2006
Hi,
Suppose I write
if x in ("abc", "def", "xyz"):
doStuff()
elif x in ("pqr", "tuv", "123"):
doOtherStuff()
elif ...
etc.
When is python building the tuples? Does it need to build the tuple
every time it comes through this code? Or does it somehow recognise
that they are constant and cache them?
In other words, is anything gained (efficiency-wise) by first putting
say
tuple1 = ("abc", "def", "xyz")
tuple2 = ("pqr", "tuv", "123")
somewhere where I know it's executed once, and then writing
if x in tuple1:
doStuff()
elif x in tuple2:
doOtherStuff()
elif ...
????
(The tuples I have in mind are of course much longer than three
elements)
Many thanks
David
More information about the Python-list
mailing list