Posts Python性能分析工具cProfile
Post
Cancel

Python性能分析工具cProfile

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?

  1. It gives you the total run time taken by the entire code.
  2. It also shows the time taken by each individual step. This allows you to compare and find which parts need optimization
  3. cProfile module also tells the number of times certain functions are being called.
  4. The data inferred can be exported easily using pstats module.
  5. 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()
    
        

Reference

cProfile – How to profile your python code

This post is licensed under CC BY 4.0 by the author.