-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtiming_function.py
More file actions
30 lines (23 loc) · 791 Bytes
/
timing_function.py
File metadata and controls
30 lines (23 loc) · 791 Bytes
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
# This timing function is cribbed from:
# https://realpython.com/blog/python/primer-on-python-decorators/.
import time
def timing_function(nested_function):
"""
Outputs the time a function takes to execute.
"""
def wrapper():
t1 = time.time()
nested_function()
t2 = time.time()
return "Time taken to run the function: " + str((t2-t1)) + "\n"
return wrapper
@timing_function
def sum_numbers():
num_list = []
for num in (range(0, 10000)):
num_list.append(num)
print("\nSum of all numbers: " + str((sum(num_list))))
# sum_numbers() makes a good demonstration of what this file can do.
# Feel free to write a function that does just about anything that you
# want to set a timing benchmark for.
print(sum_numbers())