Python 使用计时器获取函数的运行时

Python 使用计时器获取函数的运行时,python,time,timer,runtime,execution,Python,Time,Timer,Runtime,Execution,我试图了解使用time.timer运行一个函数需要多长时间,但我真的很难理解如何实现它,我认为这会起作用: def breadth_first_tree_search(problem): "Search the shallowest nodes in the search tree first." t1 = timeit.Timer(lambda: problem) n = 1 secs = t1.timeit(number = n) print ("\n%d times t

我试图了解使用time.timer运行一个函数需要多长时间,但我真的很难理解如何实现它,我认为这会起作用:

def breadth_first_tree_search(problem):
  "Search the shallowest nodes in the search tree first."
  t1 = timeit.Timer(lambda: problem)
  n = 1
  secs = t1.timeit(number = n)
  print ("\n%d times took %8f seconds" % (n,secs))
  return tree_search(problem, FIFOQueue())
但后来我意识到时机不对。
我需要它来检查
width\u first\u tree\u search
的运行时间。有人能告诉我怎么做吗?我一直觉得这并不难,但我不知道怎么做。

你有很多现成的选项来计时你的功能——这里没有必要重新发明轮子

使用ipython:
%timeit宽度\u优先\u树\u搜索(问题)

使用配置文件:

如果您确实想使用
timeit.Timer
,请按照以下示例操作


你有很多现成的选项来计时你的功能——这里没有必要重新发明轮子

使用ipython:
%timeit宽度\u优先\u树\u搜索(问题)

使用配置文件:

如果您确实想使用
timeit.Timer
,请按照以下示例操作


您可以使用装饰器启动计时器,运行real函数,并在计时器自动完成后评估计时器:

# write a decorator function taking the function to decorate as only parameter:
def timer_decorator(func):

    # define an inner function as wrapper with a flexible signature as your target function:
    def wrapper(*args, **kwargs):

        # set up timing:
        start_time = time.time()

        # call the wrapped function (passed as 'func' argument):
        return_value = func(*args, **kwargs)

        # check the timer and evaluate the time span:
        end_time = time.time()
        time_span = end_time - start_time
        print("Function '{}' took {:.3}s to run.".format(func.__name__, time_span))

        # return the decorated function's return value:
        return return_value

    # return the constructed wrapper function (don't call it --> no brackets "()" !):
    return wrapper

# Decorate your original function with your decorator:
@timer_decorator
def breadth_first_tree_search(problem):
    "Search the shallowest nodes in the search tree first."
    return tree_search(problem, FIFOQueue())

# Call your decorated function just like a normal function without any decoration:
breadth_first_tree_search("however you specify a problem...")

您可以使用装饰器启动计时器,运行real函数,并在计时器自动完成后评估计时器:

# write a decorator function taking the function to decorate as only parameter:
def timer_decorator(func):

    # define an inner function as wrapper with a flexible signature as your target function:
    def wrapper(*args, **kwargs):

        # set up timing:
        start_time = time.time()

        # call the wrapped function (passed as 'func' argument):
        return_value = func(*args, **kwargs)

        # check the timer and evaluate the time span:
        end_time = time.time()
        time_span = end_time - start_time
        print("Function '{}' took {:.3}s to run.".format(func.__name__, time_span))

        # return the decorated function's return value:
        return return_value

    # return the constructed wrapper function (don't call it --> no brackets "()" !):
    return wrapper

# Decorate your original function with your decorator:
@timer_decorator
def breadth_first_tree_search(problem):
    "Search the shallowest nodes in the search tree first."
    return tree_search(problem, FIFOQueue())

# Call your decorated function just like a normal function without any decoration:
breadth_first_tree_search("however you specify a problem...")
time.time()
可能不如Windows上的
timeit.default\u timer()
精确。请参阅Windows上的
time.time()
可能不如
timeit.default\u timer()
精确。看见