【问题标题】:Timing a Function in Python在 Python 中为函数计时
【发布时间】:2013-01-08 15:39:03
【问题描述】:

我正在尝试在 python 中为两个不同的函数计时。

第一个:

import cProfile
def bin_search(A, first,last, target):
#returns index of target in A, if present
#returns -1 if target is not present in A
if first > last:
    return -1
else:
    mid = (first+last)/2
    if A[mid]==target:
        return mid
    elif A[mid]>target:
        return bin_search(A,first,mid-1,target)
    else:
        return bin_search(A,mid+1,last,target)

第二个

def trin_search(A,first,last,target):
#returns index of target in A, if present
#returns -1 if target is not present in A
if target> last or target<first:
    return -1
if first>last:
    return -1
else:
    one_third=first+(last-first)/3
    two_thirds=first+2*(last-first)/3
    if A[one_third]==target:
        return one_third
    elif A[one_third]>target:
        #search the left-hand third
        return trin_search(A,first, one_third,target)
    elif A[two_thirds]==target:
        return two_thirds
    elif A[two_thirds]>target:
        #search the middle third
        return trin_search(A,one_third+1,two_thirds-1,target)
    else:
        #search the right-hand third
        return trin_search(A,two_thirds+1,last,target)

我正在尝试使用 cprofile.run() 方法对它们进行计时。我打电话:

cprofile.run('trin_search(newlist, newlist[0], newlist[-1], 17)')

cprofile.run('bin_search(newlist, newlist[0], newlist[-1], 17)')

第一个结果:

6 function calls (4 primitive calls) in 0.000 seconds

Ordered by: standard name

ncalls  tottime  percall  cumtime  percall filename:lineno(function)
    1    0.000    0.000    0.000    0.000 :0(setprofile)
    1    0.000    0.000    0.000    0.000 <string>:1(<module>)
  3/1    0.000    0.000    0.000    0.000 Jan 18.py:16(trin_search)
    0    0.000             0.000          profile:0(profiler)
    1    0.000    0.000    0.000    0.000 profile:0(trin_search(newlist, newlist[0], newlist[-1], 17))

第二个

7 function calls (3 primitive calls) in 0.000 seconds

Ordered by: standard name

ncalls  tottime  percall  cumtime  percall filename:lineno(function)
    1    0.000    0.000    0.000    0.000 <string>:1(<module>)
  5/1    0.000    0.000    0.000    0.000 Jan 18.py:2(bin_search)
    1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}    

他们怎么可能需要0时间来操作?

干杯,

【问题讨论】:

  • 通常情况下,最好使用timeit进行计时。

标签: python search recursion time


【解决方案1】:

正如其他使用 timeit 模块的人已经指出的那样,这里是一个如何使用参数为函数计时的示例:

import timeit

arg = 10

def foo(arg):
    return arg**arg

t=timeit.Timer("foo(arg)","from __main__ import foo, arg")
print t.timeit(5)

请注意,您必须同时导入您在函数调用中使用的函数和变量。

另外,我建议您使用IPython,您有“魔术命令”,这样您就可以简单地使用%timeit foo(arg)


对于您的示例,这应该有效:

t=timeit.Timer("bin_search(newlist, newlist[0], newlist[-1], 17)",
                       "from __main__ import bin_search, newlist") 

【讨论】:

  • 我正在尝试运行 t=timeit.Timer("bin_search(newlist, newlist[0],newlist[-1],17)","from__main__import bin_search")
  • 是的 Traceback(最近一次调用最后一次):文件“”,第 1 行,在 t=timeit.Timer("bin_search(newlist, newlist[0],newlist [-1],17)","from__main__import bin_search") 文件 "C:\Python27\lib\timeit.py",第 136 行,在 init 代码 = compile(src, dummy_src_name, "exec ") 文件 "",第 3 行 from__main__import bin_search ^ SyntaxError: invalid syntax
  • @Unknown -- 你在from, __main__, import 之间有空格,对吧?此外,字符串应该是自包含的,不引用其他对象,如 newlist
  • @Unknown -- 将您的示例添加到答案中。
  • 是的,我让它与你的例子一起工作,非常感谢你。
【解决方案2】:

试试timeit 模块。它是为对代码 sn-ps 进行基准测试而制作的。

【讨论】:

猜你喜欢
  • 2014-01-25
  • 2015-02-23
  • 1970-01-01
  • 2016-01-11
  • 2010-10-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-21
相关资源
最近更新 更多