Note that there are some explanatory texts on larger screens.

plurals
  1. POPython timing - There must be a better way!
    primarykey
    data
    text
    <p>I hope someone can help me out with this. I'd like to measure sorting algorithms. Here's how I currently do it:</p> <pre><code>M = 1000 # number of executions N = [1000, 2000, 4000, 16000] # size of the list L = [100, 1000, 2000,16000] # max element of the list # timing: 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)] # generates an n long random list t0 = time.clock() pass # sort function call goes here t1 = time.clock() t += (t1-t0) print '|\t%0.3f\t' % ((t*1000.0)/M ), # avg time print print '-'*80 </code></pre> <p>This empty test takes about 4 minutes. I would appreciate any advice on how to make it faster.</p> <p>Cheers</p> <p><strong>Edit:</strong> After Rafe Kettler's hint, I came up with this:</p> <pre><code>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 ), # avg time print print '-'*80 </code></pre> <p>Unfortunately it become slower. What am I doing wrong?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload