Sorting: too different times. Why?

Mark Dickinson dickinsm at gmail.com
Sun Nov 22 06:44:58 EST 2009


On Nov 22, 9:21 am, n00m <n... at narod.ru> 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
>
> def v_cmp(v1, v2):
>     if v1.x < v2.x and v1.y > v2.y:
>         return -1
>     return 0
>
> from random import randint
> from time import time
>
> a = []
> for i in range(200000):
>     a += [Vector(randint(0, 500000), randint(0, 500000))]
> b = a[:]
> c = a[:]
>
> print 'Sorting...'
>
> t = time()
> b.sort(cmp=v_cmp)
> print time() - t
>
> t = time()
> c.sort()
> print time() - t
>
> print b == c
>
> >>> ===================================== RESTART ======
>
> Sorting...
> 0.906000137329
> 6.57799983025
> True

Do you get the same magnitude difference if you make Vector a new-
style
class?  (I.e., use "class Vector(object)" instead of "class Vector
()".)

Mark



More information about the Python-list mailing list