Question: Decorators Let's create a Python decorator function named performance. This decorator is designed to measure the execution time of other functions. Here's how it should

Decorators
Let's create a Python decorator function named performance. This decorator is designed to measure the execution time of other functions. Here's how it should work:
Input: The performance decorator accepts a single function as its argument.
Wrapped Function: The performance decorator should return a modified ("wrapped") version of the input function that:
Records the start time before running the wrapped functionExecutes the wrapped function and stores its resturn valueRecords the end time after running the wrapped functionCalculates the elapsed time (end time - start time)
Return Value: The wrapped function should return a tuple with:
The elapsed time (in seconds) taken to execute the wrapped functionThe wrapped function's return value
The time module has a function named perf_counter for measuring how long it takes to run something. For example:
import time start_time = time.perf_counter() # "Start" timer time.sleep(1) # Wait one second end_time = time.perf_counter() # "Stop" timer elapsed_time = end_time - start_time # should be *slightly* more than 1 print(elapsed_time) # 1.0045422080000037(approximately)
Define a decorator named performance that wraps a function to return a tuple with (1) the original return value of the function and (2) the amount of time that it took. For example, we should be able to write:
@performance def add(a, b): return a + b sum_result, time_taken = add(1,2) # sum_result ==3, time_taken ~=0
Hint: to create a decorator named fake_performance that returns a tuple with the function call result and a string, we could do the following:
import functools def fake_performance(func): @functools.wraps(func) def wrapper(*args,**kwargs): result = func(*args,**kwargs) return result, "PUT THE ELAPSED TIME HERE AS A NUMBER" return wrapper

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!