- Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathpython_profiling.txt
More file actions
Latest commit
26 lines (21 loc) · 669 Bytes
/
Copy pathpython_profiling.txt
File metadata and controls
26 lines (21 loc) · 669 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
Profiling a long running python program
+++++++++++++++++++++++++++++++++++++++
1. Add the following program into source file and call as decorator
import cProfile
def do_cprofile(func):
def profiled_func(*args, **kwargs):
profile = cProfile.Profile()
try:
profile.enable()
result = func(*args, **kwargs)
profile.disable()
return result
finally:
profile.dump_stats('/tmp/profile_bin.prof')
return profiled_func
2. Convert the profile_bin.prof to human readable file
import pstats
f = open('/tmp/human_readable_profile.prof', 'w')
stats = pstats.Stats('/tmp/profile_bin.prof', stream=f)
stats.sort_stats('cumulative').print_stats()
f.close()