Discuss / Python / Homework-1

Homework-1

Topic source
def metric(text):
	def decorator(fn):
		@functools.wraps(fn)
		def wrapper(*args, **kw):
			print('Begin call %s():' % fn.__name__)
			begin = time.time()
			m = fn(*args, **kw)
			end = time.time()
			total = end - begin
			print(m)
			print('%s %s executed in %s ms' % (text, fn.__name__, total))
			return 'End call'
		return wrapper
	return decorator

@metric('Today')
def fast(x, y):
    time.sleep(0.0012)
    return x + y

@metric('Today')
def slow(x, y, z):
    time.sleep(0.1234)
    return x * y * z

print(fast(11, 22))
print()
print(slow(11, 22, 33))

  • 1

Reply