which one is more efficient
Christian Heimes
lists at cheimes.de
Sun Feb 10 01:04:32 EST 2008
ki lo wrote:
> I have type variable which may have been set to 'D' or 'E'
>
> Now, which one of following statements are more efficient
>
> if type =='D' or type == 'E':
>
> or
>
> if re.search("D|E", type):
>
> Please let me know because the function is going to called 10s of millions
> of times.
For maximum efficiency you have to use a set. You can speed up the check
even more if you rebind the set in the function declaration. It saves
the global lookup or the creation of the frozen set in every function call.
type_set=frozenset(('D', 'E'))
def yourfunction(self, ..., type_set=type_set):
...
if type in type_set:
...
...
Christian
More information about the Python-list
mailing list