Python—一个可计算函数运行时间的函数模块代码


代码封装好了可查看具体函数运行时间

具体代码

def time_function(f, *args):
    """
    Call a function f with args and return the time (in seconds) that it took to execute.
    """
    import time
    tic = time.time()
    f(*args)
    toc = time.time()
    return toc - tic

two_loop_time = time_function(classifier.compute_distances_two_loops, X_test)
print('Two loop version took %f seconds' % two_loop_time)
posted @ 2022-08-07 23:39  JaxonYe  阅读(36)  评论(0编辑  收藏  举报