Python时间函数
在用Python编写程序时经常会遇到需要求解一个函数运行时间的情况,发现一个利用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