Difference between print_callers and print_callees method of cProfile python
The print_callers and print_callees method belong to the stats class of pstats module.
print_callers():This method for the Stats class prints a list of all functions that called each function in the profiled database. The ordering is identical to that provided by print_stats(), and the definition of the restricting argument is also identical. Each caller is reported on its own line. The format differs slightly depending on the profiler that produced the stats.
In cProfile each caller is preceded by three numbers: the number of times this specific
call was made, and the total and cumulative times spent in the current function
while it was invoked by this specific caller.
print_callees():This method for the Stats class prints a list of all function that were called by
the indicated function. Aside from this reversal of direction
of calls (re: called vs was called by), the arguments and ordering are
identical to the print_callers() method.
for i in range(5):
print("List of Numbers:")
for i in range(0,20000):
if __name__ == '__main__':
import cProfile, pstats
pr = cProfile.Profile()
Sort_stats = pstats.Stats(pr)
In this code we have defined two functions and then passed it to the main function
After Profiling we have used both print_callers() and print_callees() method to print the
statistics with the restriction of 5 so that it prints only the five most
In the output first the results are printed using print_callers() method and next the
results are printed using print_callees() method where we can observe that the direction
of the calls is reversed.