Sorting: too different times. Why?
Steven D'Aprano
steve at REMOVE-THIS-cybersource.com.au
Sun Nov 22 06:04:19 EST 2009
In the subject line, you write "too different times". You actually want
"two", the number, not "too" as in "too many", "too much". Lots of native
English speakers get this wrong too :)
On Sun, 22 Nov 2009 01:21:42 -0800, n00m wrote:
> Any comment:
>
> class Vector:
> def __init__(self, x, y):
> self.x = x
> self.y = y
> def __cmp__(self, v):
> if self.x < v.x and self.y > v.y:
> return -1
> return 0
Modern versions of Python (since 2.2 I think?) use __lt__ or __gt__ for
sorting. If the class doesn't have a __lt__ method, Python falls back on
__cmp__.
> b.sort(cmp=v_cmp)
This is relatively fast, because you pass a comparison function directly,
so Python doesn't need to look for a __lt__ method then fall back to
__cmp__. It just uses v_cmp, every time.
> c.sort()
This is slower, because every comparison looks up the __lt__ and fails,
then tries the __cmp__.
If you change the definition of Vector to include rich comparison methods
as detailed here:
http://docs.python.org/reference/datamodel.html#object.__lt__
sorting will probably be significantly faster still.
--
Steven
More information about the Python-list
mailing list