Skip to content

Commit 07b8958

Browse files
committed
2 parents 75d02bf + 43bc404 commit 07b8958

1 file changed

Lines changed: 26 additions & 0 deletions

File tree

python_profiling.txt

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
Profiling a long running python program
2+
+++++++++++++++++++++++++++++++++++++++
3+
1. Add the following program into source file and call as decorator
4+
5+
import cProfile
6+
7+
def do_cprofile(func):
8+
def profiled_func(*args, **kwargs):
9+
profile = cProfile.Profile()
10+
try:
11+
profile.enable()
12+
result = func(*args, **kwargs)
13+
profile.disable()
14+
return result
15+
finally:
16+
profile.dump_stats('/tmp/profile_bin.prof')
17+
return profiled_func
18+
19+
2. Convert the profile_bin.prof to human readable file
20+
21+
import pstats
22+
23+
f = open('/tmp/human_readable_profile.prof', 'w')
24+
stats = pstats.Stats('/tmp/profile_bin.prof', stream=f)
25+
stats.sort_stats('cumulative').print_stats()
26+
f.close()

0 commit comments

Comments
 (0)