how fast is Python code - another detail

Andrew Koenig ark at acm.org
Thu Mar 4 14:43:40 EST 2004


"Peter Otten" <__peter__ at web.de> wrote in message
news:c280b0$1g3$03$1 at news.t-online.com...

> key in theDict.keys()

> changes the lookup from constant time (a hash value is calculated which is
> used as an index into a list of values if all goes well) to proportonial
to
> len(theDict) because on average half of the list has to be searched before
> the matching entry is found.

Worse:  Evaluating theDict.keys() requires building a list from all the
keys.  So even if the key is the first element of the list, the run time is
O(len(theDict)).

Moreover, there's no way for the compiler to optimize such expressions in
general, because it doesn't know the type of theDict during compilation.





More information about the Python-list mailing list