-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexample_analyze.py
More file actions
92 lines (67 loc) · 2.05 KB
/
example_analyze.py
File metadata and controls
92 lines (67 loc) · 2.05 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
from analyze import analyze_number_of_calls, analyze_time_of_execution, print_analysis, restart_analysis
""" Shorter:
How about a optimizing tool for your python project that keeps track of
the number of times you called a specific function and the total time the
function took.
Short:
@analyze_number_of_calls decorator adds functions to a hidden dictionary
object which tracks numbers of their calls in your application.
@analyze_time_of_execution decorator gets total number of seconds the function
took to execute.
This way you can keep track of which functions in your application should
be optimized.
print_analysis() prints out functions ordered by their call number and time took
restart_analysis() clears the dictionary and starts analysis from the start
"""
class AClass:
@analyze_number_of_calls
def __init__(self):
pass
@analyze_number_of_calls
def nothing(self):
pass
class BClass:
@analyze_number_of_calls
def __init__(self):
pass
@analyze_number_of_calls
def nothing(self):
pass
@analyze_number_of_calls
def some_function():
pass
item1 = AClass()
item2 = AClass()
item1.nothing()
# PRINT IT
print_analysis()
print("-----------------------|||||||||||||||----------------------------------")
item3 = BClass()
item3.nothing()
item3.nothing()
item3.nothing()
item3.nothing()
# PRINT IT
print_analysis()
print("-----------------------|||||||||||||||----------------------------------")
restart_analysis()
some_function()
some_function()
some_function()
some_function()
some_function()
some_function()
# PRINT IT
print_analysis()
print("-----------------------|||||||||||||||----------------------------------")
restart_analysis()
@analyze_time_of_execution
def this_function_takes_some_time():
_ = 0
for item in range(5000):
for __ in range(item):
_ = _ + 1
this_function_takes_some_time()
print_analysis()
this_function_takes_some_time()
print_analysis()