Introduction to cProfile
cProfile
is a built-in python module that can perform profiling. It is the most commonly used profiler currently.
But, why cProfile
is preferred?
- It gives you the total run time taken by the entire code.
- It also shows the time taken by each individual step. This allows you to compare and find which parts need optimization
- cProfile module also tells the number of times certain functions are being called.
- The data inferred can be exported easily using
pstats
module. - The data can be visualized nicely using
snakeviz
module. Examples come later in this post.
Example
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
def create_array():
array=[]
for i in range(400000):
arr.append(i)
def print_statement():
print('Array created successfully!')
def main():
create_array()
print_statement()
if __name__ == '__main__':
import cProfile, pstats
for i in range(5):
profiler = cProfile.Profile()
profiler.enable()
main()
profiler.disable()
stats = pstats.Stats(profiler).sort_stats('ncalls')
stats.dump_stats('result.out') # 用snakeviz result.out 可以生成网页版调用图, pip install snakeviz
stats.print_stats()