小编stu*_*stu的帖子

如何多次运行一个函数

我必须在1000次运行中找到我的函数运行时间的平均值.我应该使用哪些代码使其运行1000次然后找到它们的平均值?我的功能是:

import time

t0 = time.clock()

def binary_search(my_list, x):

    left=0
    right=len(my_list)-1
    while left<=right:
        mid = (left+right)//2
        if my_list[mid]==x:
            return True
        elif my_list[mid] < x: #go to right half
            left = mid+1
        else:              #go to left half
            right = mid-1
    return False  #if we got here the search failed
t1 = time.clock()

print("Running time: ", t1-t0, "sec")
Run Code Online (Sandbox Code Playgroud)

python python-2.7 python-3.x

3
推荐指数
1
解决办法
3165
查看次数

标签 统计

python ×1

python-2.7 ×1

python-3.x ×1