Hope someone can help me. I would like to measure sorting algorithms. Here's how I do it now:
M = 1000
N = [1000, 2000, 4000, 16000]
L = [100, 1000, 2000,16000]
print 'Number of executions: %i' % (M)
print '-'*80
print '\tL\N\t|\t%i\t|\t%i\t|\t%i\t|\t%i' % (N[0], N[1], N[2], N[3])
print '-'*80
for l in L:
print '\t%i\t' % l,
for n in N:
t = 0
for m in xrange(M):
A = [random.randint(0,l-1) for r in xrange(n)]
t0 = time.clock()
pass
t1 = time.clock()
t += (t1-t0)
print '|\t%0.3f\t' % ((t*1000.0)/M ),
print
print '-'*80
This empty test takes about 4 minutes. I would appreciate any advice on how to make this faster.
Greetings
Edit:
After the hint of Rafe Kettler, I came up with the following:
def sorting(LST):
pass
if __name__ == "__main__" :
M = 1000
N = [1000, 2000, 4000, 16000]
L = [100, 1000, 2000,16000]
print 'Number of executions: %i' % (M)
print '-'*80
print '\tL\N\t|\t%i\t|\t%i\t|\t%i\t|\t%i' % (N[0], N[1], N[2], N[3])
print '-'*80
for l in L:
print '\t%i\t' % l,
for n in N:
t = timeit.Timer('sorting([random.randint(0,l-1) for r in xrange(n)])', 'from __main__ import sorting, n, l, random')
print '|\t%0.3f\t' % (t.timeit(M)/M ),
print
print '-'*80
Unfortunately, it is getting slower. What am I doing wrong?