Sorting: too different times. Why?

n00m n00m at narod.ru
Sun Nov 22 04:21:42 EST 2009


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




More information about the Python-list mailing list