- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython_profiling.py
More file actions
Latest commit
95 lines (77 loc) · 2.87 KB
/
Copy pathpython_profiling.py
File metadata and controls
95 lines (77 loc) · 2.87 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
92
93
94
95
'''
In this script we are going to see how well the python code is working based on time and memory
to do so we use profiling and optimization
we will use the cProfile module for time profiling and memory_profiler for profiling memory usege of each funtion
For memory profiling we use the memory_profiler module so:
we install it with: pip install memory_profiler
Author: Morteza Heidari
Made in: 10/13/2022
'''
importcProfile
importpstats
importio
frommemory_profilerimportprofile
importnumpyasnp
####
# -------------------------------------------------------------------------
# the function below is used for time profiling, all you need to do is put it on top of the main function you want to profile
# and it returns the time the function and all the subfunctions took to execute
# -------------------------------------------------------------------------
# it has a decorator @time_profiling that takes another function as an argument
deftime_profiling(fnc):
"""A decorator that uses cProfile to profile a function"""
definner(*args, **kwargs):
pr=cProfile.Profile() # in that we start the profiler
pr.enable()
retval=fnc(*args, **kwargs) # we execute the function
pr.disable() # then we stop the profiler
s=io.StringIO() # we create a string buffer
sortby='cumulative'
ps=pstats.Stats(pr, stream=s).sort_stats(sortby)
ps.print_stats()
print(s.getvalue()) # we print the result
returnretval# we return the result of the function
returninner
# -------------------------------------------------------------------------
# make a couple of examples to see how it works
@time_profiling# this is for profiling the code and see how much time it takes to execute
@profile# this is for profiling the memory usege of the function
defexample1():
"""This function is an example of a function that is not optimized"""
a= [i**2foriinrange(10000)]
# first define a numpy array of 10000 zero elements as b
b=np.zeros(10000)
foriinrange(10000):
b[i] =i**2
j=0
foriinrange(1000):
j=j+i
m=example2()
d=example3()
returni, m
@profile
defexample2():
"""This function is an example of a function that is optimized"""
m=0
n=1468.4345
foriinrange(1000):
forjinrange(1000):
m+=i*j
m=m+n
q=m/10
s=m*10000
returnm
@profile
defexample3():
""" we define a dictionary that contains the numbers from 0 to 9 as keys and the number of times they occur as values"""
d= {i: 0foriinrange(10)}
foriinrange(10000):
d[i%10] +=12313453.5677643
# s = d * 2
returnd
# now we are going to do memory profiling
# we will use the memory_profiler module
#
if__name__=='__main__':
# find_duplicate_movies()
example1()