[Python-ideas] set.add() return value

Raymond Hettinger python at rcn.com
Tue Feb 17 22:03:40 CET 2009


>> One result was that two successive accesses for the
>> same key in a large table did *not* double the time.
> 
> What kind of keys, though? In my case the keys aren't
> strings, they're tuples. Even after you've found the
> right slot in the hash table, you still need to compare
> the tuples.

Even if the hash has to be recomputed, processor
caching makes the second pass very cheap compared
to the first.

I spent a month trying to optimize dicts and learned
that this is a dead-end.

It is certainly *not* worth mucking-up the API for sets.

Even if it did improve you one app, that would be
atypical.  Most apps that test and add will do 
something inside the branch that consumes far more
time than the contains test. 


Raymond



More information about the Python-ideas mailing list