[Patches] [ python-Patches-597444 ] Speed-up sets
noreply@sourceforge.net
noreply@sourceforge.net
Tue, 20 Aug 2002 15:25:02 -0700
Patches item #597444, was opened at 2002-08-19 17:57
You can respond by visiting:
https://sourceforge.net/tracker/?func=detail&atid=305470&aid=597444&group_id=5470
Category: Library (Lib)
Group: Python 2.3
Status: Open
Resolution: None
Priority: 5
Submitted By: Raymond Hettinger (rhettinger)
>Assigned to: Guido van Rossum (gvanrossum)
Summary: Speed-up sets
Initial Comment:
Gained a 5:1 speed-up for membership testing by
handling the most common case first (the case
where the element is hashable).
Also, renamed the popitem() method to pop() for
consistency with list.pop and dict.pop(k). The 'item'
in popitem suggests that a tuple will be returned.
----------------------------------------------------------------------
Comment By: Raymond Hettinger (rhettinger)
Date: 2002-08-20 17:24
Message:
Logged In: YES
user_id=80475
Revised patch attached.
----------------------------------------------------------------------
Comment By: Raymond Hettinger (rhettinger)
Date: 2002-08-20 17:00
Message:
Logged In: YES
user_id=80475
Will do.
----------------------------------------------------------------------
Comment By: Guido van Rossum (gvanrossum)
Date: 2002-08-20 16:52
Message:
Logged In: YES
user_id=6380
I've implemented the popitem -> pop rename in CVS now.
Can you submit a reworked patch for the rest?
----------------------------------------------------------------------
Comment By: Guido van Rossum (gvanrossum)
Date: 2002-08-20 07:38
Message:
Logged In: YES
user_id=6380
Close. I agree with pop().
I agree that you can catch TypeError instead of being
prudent, but please use this idiom, which preserves the
traceback:
>>> try:
>>> return element in self._data
>>> except TypeError:
>>> transform = getattr(element,
"_as_temporary_immutable", None)
>>> if transform is not None:
>>> return transform() in self._data
>>> else:
>>> raise # re-raise the TypeError exception we caught
This *may* even be faster, because the AttributeError is
caught in C and never instantiated. But I don't care about
the speed of this so much as I care about preserving the
traceback -- and also about not hiding an AttributeError
that might happen inside the transform() call.
----------------------------------------------------------------------
Comment By: Raymond Hettinger (rhettinger)
Date: 2002-08-19 17:59
Message:
Logged In: YES
user_id=80475
Attached a script that verifies the claimed timing speed-up.
----------------------------------------------------------------------
You can respond by visiting:
https://sourceforge.net/tracker/?func=detail&atid=305470&aid=597444&group_id=5470