cProfile - Python performance analysis tool
Excerpt: https://www.cnblogs.com/kaituorensheng/p/4453953.html
Python module comes with several performance analysis: profile, cProfile and hotshot, use basically the same, nothing more than pure module is written in C or Python. This article describes cProfile.
example
import time def func1(): sum = 0 for i in range(1000000): sum += i def func2(): time.sleep(10) func1() func2()
run
python -m cProfile del.py
operation result
The results of analysis
performed six functions, spent a total of 10.138s, run by pressing the function name to sort the output results.
Run the script
python -m cProfile -o del.out del.py
In this module directly stored profile result, the output may be further analyzed, running
python -c "import pstats; p=pstats.Stats('del.out'); p.print_stats()"
The results (random)
You can set sort, such as how much to spend time sorting
python -c "import pstats; p=pstats.Stats('del.out'); p.sort_stats('time').print_stats()"
sort_stats supports the following parameters:
1
|
calls, cumulative, file, line, module, name, nfl, pcalls, stdname, time
|
pstats module also supports interactive
Python module comes with several performance analysis: profile, cProfile and hotshot, use basically the same, nothing more than pure module is written in C or Python. This article describes cProfile.
example
import time def func1(): sum = 0 for i in range(1000000): sum += i def func2(): time.sleep(10) func1() func2()
run
python -m cProfile del.py
operation result
The results of analysis
performed six functions, spent a total of 10.138s, run by pressing the function name to sort the output results.
Run the script
python -m cProfile -o del.out del.py
In this module directly stored profile result, the output may be further analyzed, running
python -c "import pstats; p=pstats.Stats('del.out'); p.print_stats()"
The results (random)
You can set sort, such as how much to spend time sorting
python -c "import pstats; p=pstats.Stats('del.out'); p.sort_stats('time').print_stats()"
sort_stats supports the following parameters:
1
|
calls, cumulative, file, line, module, name, nfl, pcalls, stdname, time
|
pstats module also supports interactive