Python计时——一定有更好的方法!
我希望有人能帮助我解决这个问题。 我想测量排序算法。我目前是这样做的:
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
这个空测试大约需要 4 分钟。我将不胜感激任何有关如何使其更快的建议。
干杯
编辑: 在 Rafe Kettler 的提示后,我想到了这一点:
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
不幸的是它变得更慢。我做错了什么?
I hope someone can help me out with this.
I'd like to measure sorting algorithms. Here's how I currently do it:
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
This empty test takes about 4 minutes. I would appreciate any advice on how to make it faster.
Cheers
Edit:
After Rafe Kettler's hint, I came up with this:
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
Unfortunately it become slower. What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
timeit。 Python 中计时的最佳方式。将算法重构为函数并使用 timeit 来测试执行时间。
timeit. Best way to time in Python, period. Refactor your algorithms into functions and use
timeit
to test the execution time.您可以替换此代码:
用生成器?例如
,我认为,空测试中的大部分时间都是随机列表生成
It is possible for you replace this code:
With generator? eg
I think, most of time in your empty test is random list generation
创建随机数是一项耗时的任务。您正在创建 4*1000*(1000+2000+4000+16000) 个。最简单的测试用例在我的系统上需要花费超过 7 分钟的时间:
正如我在评论中所说,将创建测试数据的时间从被测算法的时间中排除是非常重要的。
Creating random numbers is a time-consuming task. You're creating 4*1000*(1000+2000+4000+16000) of them. The simplest possible test case takes over 7 minutes on my system:
As I said in a comment, it's extremely important to exclude the timings for creating your test data from the timings of the algorithm under test.
生成一次随机数。将它们放在架子或泡菜文件中,然后在需要运行测试时将其读出。
Generate the random numbers once. Put them in a shelve or pickle file and then read them out when you need to run a test.
没有完全回答 timimg 问题,但您可以使用 numpy 包中的 random 模块非常有效地生成大量随机数:
调整OP的脚本,下面是使用 numpy.random 与 stock random 模块的总时间比较:
Not quite answering the timimg question, but you can use the random module in numpy package to generate large array of random numbers very effeciently:
Adapting OP's script, below is comparision of total time using numpy.random v.s. stock random module: