Along my source code I try to capture and measure the time release of a segment in Python. How can I measure that segment pass time in a convenient way with good precision?
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Evil ctypes hack in python
- Correctly parse PDF paragraphs with Python
Use a profiler.
Python's
cProfile
is included in the standard libary.For an even more convenient way, use the package
profilestats
. Then you can use a decorator to just decorate the functions you want to profile:This will cause a summary like this to be printed on STDOUT:
The much more useful info however is in the
cachegrind.out.profilestats
file generated. You can open this file with a tools that can visualize cachegrind statistics, for example RunSnakeRun and get nice, easy (or easier) to read visualizations of your call stack like this:Update: Both pull requests for
profilestats
andpyprof2calltree
have been merged, so they now support Python 3.x as well.