The class Stats (the above code just created an instance of this class) has a variety of methods for manipulating and printing the data that was just read into p. When you ran cProfile.run() above, what was printed was the result of three method calls:
The first method removed the extraneous path from all the module names. The second method sorted all the entries according to the standard module/line/name string that is printed. The third method printed out all the statistics. You might try the following sort calls:
The first call will actually sort the list by function name, and the second call will print out the statistics. The following are some interesting calls to experiment with:
This sorts the profile by cumulative time in a function, and then only prints the ten most significant lines. If you want to understand what algorithms are taking time, the above line is what you would use.
If you were looking to see what functions were looping a lot, and taking a lot of time, you would do:
to sort according to time spent within each function, and then print the statistics for the top ten functions.
You might also try:
This will sort all the statistics by file name, and then print out statistics for only the class init methods (since they are spelled with __init__ in them). As one final example, you could try:
This line sorts statistics with a primary key of time, and a secondary key of cumulative time, and then prints out some of the statistics. To be specific, the list is first culled down to 50% (re: .5) of its original size, then only lines containing init are maintained, and that sub-sub-list is printed.
If you wondered what functions called the above functions, you could now (p is still sorted according to the last criteria) do:
and you would get a list of callers for each of the listed functions.
If you want more functionality, you’re going to have to read the manual, or guess what the following functions do: