Python计时——一定有更好的方法!

发布于 2024-10-02 04:43:39 字数 1563 浏览 2 评论 0原文

我希望有人能帮助我解决这个问题。 我想测量排序算法。我目前是这样做的:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

想念有你 2024-10-09 04:43:39

timeit。 Python 中计时的最佳方式。将算法重构为函数并使用 timeit 来测试执行时间。

timeit. Best way to time in Python, period. Refactor your algorithms into functions and use timeit to test the execution time.

单身情人 2024-10-09 04:43:39

您可以替换此代码:

A = [random.randint(0,l-1) for r in xrange(n)]

用生成器?例如

def A(n):
    for r in xrange(n):
        yield random.randint(0,l-1)

,我认为,空测试中的大部分时间都是随机列表生成

It is possible for you replace this code:

A = [random.randint(0,l-1) for r in xrange(n)]

With generator? eg

def A(n):
    for r in xrange(n):
        yield random.randint(0,l-1)

I think, most of time in your empty test is random list generation

掀纱窥君容 2024-10-09 04:43:39

创建随机数是一项耗时的任务。您正在创建 4*1000*(1000+2000+4000+16000) 个。最简单的测试用例在我的系统上需要花费超过 7 分钟的时间:

>>> t=timeit.Timer('random.randint(0,15999)','import random')
>>> t.timeit(4*1000*(1000+2000+4000+16000))
447.08869618904077

正如我在评论中所说,将创建测试数据的时间从被测算法的时间中排除是非常重要的。

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:

>>> t=timeit.Timer('random.randint(0,15999)','import random')
>>> t.timeit(4*1000*(1000+2000+4000+16000))
447.08869618904077

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.

美人迟暮 2024-10-09 04:43:39

生成一次随机数。将它们放在架子或泡菜文件中,然后在需要运行测试时将其读出。

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.

逆光飞翔i 2024-10-09 04:43:39

没有完全回答 timimg 问题,但您可以使用 numpy 包中的 random 模块非常有效地生成大量随机数:

>>> from numpy import random
>>> l = 100; n = 16000
>>> random.randint(0,l-1,n)

调整OP的脚本,下面是使用 numpy.random 与 stock random 模块的总时间比较:

numpy.random
number of executions: 1000
--------------------------------------------------------------------------------
        L\N     |       1000    |       2000    |       4000    |       16000
--------------------------------------------------------------------------------
        100     |       0.022   |       0.043   |       0.084   |       0.332
        1000    |       0.016   |       0.031   |       0.059   |       0.231
        2000    |       0.016   |       0.030   |       0.059   |       0.231
        16000   |       0.016   |       0.030   |       0.059   |       0.231
--------------------------------------------------------------------------------

random 
Number of executions: 1000
--------------------------------------------------------------------------------
        L\N     |       1000    |       2000    |       4000    |       16000
--------------------------------------------------------------------------------
        100     |       2.152   |       4.271   |       8.649   |       34.007
        1000    |       2.264   |       4.501   |       8.762   |       34.956
        2000    |       2.202   |       4.412   |       8.743   |       34.818
        16000   |       2.205   |       4.398   |       8.735   |       34.823
--------------------------------------------------------------------------------

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:

>>> from numpy import random
>>> l = 100; n = 16000
>>> random.randint(0,l-1,n)

Adapting OP's script, below is comparision of total time using numpy.random v.s. stock random module:

numpy.random
number of executions: 1000
--------------------------------------------------------------------------------
        L\N     |       1000    |       2000    |       4000    |       16000
--------------------------------------------------------------------------------
        100     |       0.022   |       0.043   |       0.084   |       0.332
        1000    |       0.016   |       0.031   |       0.059   |       0.231
        2000    |       0.016   |       0.030   |       0.059   |       0.231
        16000   |       0.016   |       0.030   |       0.059   |       0.231
--------------------------------------------------------------------------------

random 
Number of executions: 1000
--------------------------------------------------------------------------------
        L\N     |       1000    |       2000    |       4000    |       16000
--------------------------------------------------------------------------------
        100     |       2.152   |       4.271   |       8.649   |       34.007
        1000    |       2.264   |       4.501   |       8.762   |       34.956
        2000    |       2.202   |       4.412   |       8.743   |       34.818
        16000   |       2.205   |       4.398   |       8.735   |       34.823
--------------------------------------------------------------------------------
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文