就改个函数名就不能运行了

原函数:

import time


def performance(unit):

    def perf_decorator(f):

        def wrapper(*args, **kw):

            t1 = time.time()

            r = f(*args, **kw)

            t2 = time.time()

            t = (t2 - t1)*1000 if unit =='ms' else (t2 - t1)

            print 'call %s() in %f %s'%(f.__name__, t, unit)

            return r

        return wrapper

    return perf_decorator

改完以后:

@perfor

import time

def performance(unit):

    def g(r):

def wa(*args,**kw):

r1=time.time()

f=r(*args,**kw)

r2=time.time()

t=(r2-r1)*1000 if unit=='ms' else(r2-r1)

print 'call %s() in %f %s'%(r.__name__, t, unit)

return f

return wa

return g


@performance('ms')  

def factorial(n):

    return reduce(lambda x,y: x*y, range(1, n+1))


print factorial(10)


qq_殘夜_0
浏览 1244回答 0
0回答
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python