I’m writing some program with sorting algorithms comparsion using Python. I want to measure average sorting time. I have a problem with first measurement.
This:
for i in xrange(self.repeats):
# random list generator
data_orig = [random.randint(0, self.size - 1) for x in xrange(self.size)]
sorter = self.class_()
data = data_orig[:]
debug("%s for data size: %d, try #%d" % (sorter.__class__.__name__, self.size, i+1))
t1 = time.clock()
sorter.sort(data)
t2 = time.clock()
debug("Took: %0.4fms, shifts: %d, comparisons: %d" % ((t2-t1)*1000.0, sorter.shifts, sorter.comps))
class_ is a reference to InsertionSort class.
For size = 1000 and 5 repeats I get following results:
InsertionSort for data size: 1000, try #1
Took: 39.5341ms, shifts: 254340, comparisons: 255331
InsertionSort for data size: 1000, try #2
Took: 6.0765ms, shifts: 250778, comparisons: 251772
InsertionSort for data size: 1000, try #3
Took: 6.9946ms, shifts: 254189, comparisons: 255180
InsertionSort for data size: 1000, try #4
Took: 6.7421ms, shifts: 252162, comparisons: 253156
InsertionSort for data size: 1000, try #5
Took: 5.9584ms, shifts: 241412, comparisons: 242404
For every sorting algorithm and every time I run program first result is bigger than the others. I run it with PyPy (with Python it seems OK, but it’s MUCH slower).
I know I can simply ommit first results but this solution doesn’t satisfies me 🙂
Any ideas?
Because that’s the whole point of PyPy. It’s an optimizing just-in-time compiler, which means that the more you run a piece of code, the more optimized it gets. The first time you run it, it hasn’t had a chance to do any optimization, so the result will be slow. Subsequent runs will take into account the lessons learned from the first time, so will be faster.