Bruno Desthuilliers wrote: > Paul Rubin a écrit : >>> 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. >> if type in 'DE': ... >> >> might be faster. > > It might also be wrong if there's a possibility that type=='DE' : > >>> "DE" in "DE" > True > > If the "type" variable really is a type, of course, then the real solution is hardly radical: stop using the names of types and start using the types:
>>> class D(object): pass ... >>> type(D) <type 'type'> >>> D <class '__main__.D'> >>> d = D() >>> type(d) <class '__main__.D'> >>> class E(object): pass ... >>> e = E() >>> o = object() >>> for c in d, e, o: ... print c, (type(c) in (D, E)) ... <__main__.D object at 0x7ff3150c> True <__main__.E object at 0x7ff3156c> True <object object at 0x7ff81478> False >>> regards Steve -- Steve Holden +1 571 484 6266 +1 800 494 3119 Holden Web LLC http://www.holdenweb.com/ -- http://mail.python.org/mailman/listinfo/python-list