how fast is Python code - another detail

Skip Montanaro skip at pobox.com
Thu Mar 4 14:46:58 EST 2004


    >>> if sig not in d.keys():

vs.

    >> if sig not in d.iterkeys():

vs.

    if sig not in d:

The first must build the list of keys then search it sequentially.  The
second doesn't have to build the entire list, but still searches
sequentially.  The third effectively tries d[sig] and catches the KeyError
exception of sig is not a key in d.  The third is O(1) in the general case.
The others are O(len(d)).

Skip




More information about the Python-list mailing list