Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sort cProfile output by percall when profiling a Python script

I'm using python -m cProfile -s calls myscript.py

python -m cProfile -s percall myscript.py does not work.

The Python documentation says "Look in the Stats documentation for valid sort values.": http://docs.python.org/library/profile.html#module-cProfile, which I cannot find.

like image 313
Brandon O'Rourke Avatar asked Apr 26 '12 03:04

Brandon O'Rourke


People also ask

How do you profile a Python code using cProfile?

The syntax is cProfile. run(statement, filename=None, sort=-1) . You can pass python code or a function name that you want to profile as a string to the statement argument. If you want to save the output in a file, it can be passed to the filename argument.

How do you do profiling in Python?

Method 3: Python cProfile Python includes a built-in module called cProfile which is used to measure the execution time of a program. The cProfiler module provides all information about how long the program is executing and how many times the function gets called in a program.


1 Answers

-s only uses the keys found under sort_stats.

calls (call count) cumulative (cumulative time) cumtime (cumulative time) file (file name) filename (file name) module (file name) ncalls (call count) pcalls (primitive call count) line (line number) name (function name) nfl (name/file/line) stdname (standard name) time (internal time) tottime (internal time) 

Here's an example

python -m cProfile -s tottime myscript.py 
like image 180
Joshua Olson Avatar answered Sep 19 '22 21:09

Joshua Olson