MCPcopy Index your code
hub / github.com/python/cpython / print_stats

Method print_stats

Lib/profiling/sampling/pstats_collector.py:88–255  ·  view source on GitHub ↗

Print formatted statistics to stdout.

(self, sort=-1, limit=None, show_summary=True, mode=None)

Source from the content-addressed store, hash-verified

86 )
87
88 def print_stats(self, sort=-1, limit=None, show_summary=True, mode=None):
89 """Print formatted statistics to stdout."""
90 # Create stats object
91 stats = pstats.SampledStats(self).strip_dirs()
92 if not stats.stats:
93 print("No samples were collected.")
94 if mode == PROFILING_MODE_CPU:
95 print("This can happen in CPU mode when all threads are idle.")
96 return
97
98 # Get the stats data
99 stats_list = []
100 for func, (
101 direct_calls,
102 cumulative_calls,
103 total_time,
104 cumulative_time,
105 callers,
106 ) in stats.stats.items():
107 stats_list.append(
108 (
109 func,
110 direct_calls,
111 cumulative_calls,
112 total_time,
113 cumulative_time,
114 callers,
115 )
116 )
117
118 # Calculate total samples for percentage calculations (using direct_calls)
119 total_samples = sum(
120 direct_calls for _, direct_calls, _, _, _, _ in stats_list
121 )
122
123 # Sort based on the requested field
124 sort_field = sort
125 if sort_field == -1: # stdname
126 stats_list.sort(key=lambda x: str(x[0]))
127 elif sort_field == 0: # nsamples (direct samples)
128 stats_list.sort(key=lambda x: x[1], reverse=True) # direct_calls
129 elif sort_field == 1: # tottime
130 stats_list.sort(key=lambda x: x[3], reverse=True) # total_time
131 elif sort_field == 2: # cumtime
132 stats_list.sort(key=lambda x: x[4], reverse=True) # cumulative_time
133 elif sort_field == 3: # sample%
134 stats_list.sort(
135 key=lambda x: (x[1] / total_samples * 100)
136 if total_samples > 0
137 else 0,
138 reverse=True, # direct_calls percentage
139 )
140 elif sort_field == 4: # cumul%
141 stats_list.sort(
142 key=lambda x: (x[2] / total_samples * 100)
143 if total_samples > 0
144 else 0,
145 reverse=True, # cumulative_calls percentage

Calls 7

_determine_best_unitMethod · 0.95
_print_summaryMethod · 0.95
strFunction · 0.85
strip_dirsMethod · 0.80
itemsMethod · 0.45
appendMethod · 0.45
sortMethod · 0.45