类装饰器:
import time
# 用作decorator
class duration(object):
def __init__(self, func):
self.func = func
def __call__(self, *args, **kw):
print 'start running'
start = time.clock()
afunc = self.func(*args, **kw)
end = time.clock()
print 'end running'
return afunc, 'running time is %f' % (end - start)
class computer(object):
@duration
def mutiple(self, countdown):
# time.sleep(1)
while countdown > 0:
countdown = countdown - 1
return countdown
ob = computer()
res = computer.mutiple(ob, 100000)
print res