how fast is Python code - another detail

Gregor Lingl glingl at aon.at
Thu Mar 4 14:47:11 EST 2004



Peter Otten schrieb:
> Gregor Lingl wrote:
> 
> I think you do not understand. The change from
> 
> key in theDict # old style would be theDict.has_key(key)
> 
> to
> 
> # iterkeys() can sometimes be used to avoid the creation of 
> # the list with all keys, you would have to introduce a for loop.
> # But keys vs iterkeys is not the relevant difference here
> key in theDict.keys()                       
> 
Ah, yes, I didn't think of this. This is indeed nearly as
fast as key in dict.

> 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.
Yes, this is what I meant in my first posting

Thanks
Gregor
> 
> Peter



More information about the Python-list mailing list