-
Notifications
You must be signed in to change notification settings - Fork 0
/
decorator_test.py
38 lines (30 loc) · 1018 Bytes
/
decorator_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
from functools import wraps
import time
def timeit(func):
@wraps(func)
def timeit_wrapper(*args, **kwargs):
start_time = time.perf_counter()
result = func(*args, **kwargs)
end_time = time.perf_counter()
total_time = end_time - start_time
# first item in the args, ie `args[0]` is `self`
print(f"Function {func.__name__}{args} {kwargs} Took {total_time:.4f} seconds")
return result
return timeit_wrapper
class Calculator:
@timeit
def calculate_something(self, num):
"""
an example function that returns sum of all numbers up to the square of num
"""
total = sum((x for x in range(0, num**2)))
return total
def __repr__(self):
return f"calc_object:{id(self)}"
if __name__ == "__main__":
calc = Calculator()
calc.calculate_something(10)
calc.calculate_something(100)
calc.calculate_something(1000)
calc.calculate_something(5000)
calc.calculate_something(10000)