[Python-Dev] Faster Set.discard() method?

Tony Meyer t-meyer at ihug.co.nz
Fri Mar 18 07:04:06 CET 2005


>>> But the dict.pop method is about 12 times faster. Is this 
>>> worth doing?
>>
>> The 2.4 builtin set's discard function looks like it does 
>> roughly the same as the 2.3 sets.Set.  Have you tried comparing
>> a C version of your version with the 2.4 set to see if there are
>> speedups there, too?
> 
> Ah. I had forgotten it was builtin - I'd found the python 
> implementation and concluded the C implementation didn't make
> it into 2.4 for some reason... 8-)
> 
> Yes, the builtin set.discard() method is already faster than 
> dict.pop().

The C implementation has this code:

"""
	if (PyDict_DelItem(so->data, item) == -1) {
		if (!PyErr_ExceptionMatches(PyExc_KeyError))
			return NULL;
		PyErr_Clear();
	}
"""

Which is more-or-less the same as the sets.Set version, right?  What I was
wondering was whether changing that C to a C version of your dict.pop()
version would also result in speedups.  Are Exceptions really that slow,
even at the C level?

=Tony.Meyer



More information about the Python-Dev mailing list