一个测量python 函数运行时间的 decorator

阅读更多
#coding=utf-8
import timeit,math
class TimeMeth(object):
    def __init__(self, orig_func):
        self.f = orig_func
   
    def __call__(self, *args, **kwargs):
        start = timeit.default_timer()
        r = self.f(*args, **kwargs)
        end = timeit.default_timer()
        print "The running time for %s:%2f"%(self.f.__name__, end-start)
        return r

@TimeMeth
def f(n):
    r = 0
    for i in range(1, n):
        t = math.factorial(i)
        r = r + t
       
    return r

if __name__ == '__main__':
    f(5000)

请指教

你可能感兴趣的:(Python,F#)