Since not everyone has PyCharm Pro which can measure script's running time, here is a simple solution that uses decorator. We only need to add a single line of code to measure the running time of any function as follows:
import time
def timeit(func):
"""
Decorator for measuring function's running time.
"""
def measure_time(*args, **kw):
start_time = time.time()
result = func(*args, **kw)
print("Processing time of %s(): %.2f seconds."
% (func.__qualname__, time.time() - start_time))
return result
return measure_time
@timeit
def func():
for _ in range(3):
time.sleep(1)
if __name__ == "__main__":
func()
Output:
Processing time of func(): 3.00 seconds.