@@ -114,6 +114,12 @@ def reset(self) -> None:
114
114
"""
115
115
...
116
116
117
+ def _make_report (self ) -> TimerReport :
118
+ """
119
+ Creates a report of timing data.
120
+ """
121
+ ...
122
+
117
123
118
124
class Timer (TimerProtocol ):
119
125
def __init__ (
@@ -176,6 +182,31 @@ def reset(self) -> None:
176
182
"""
177
183
self .recorded_durations = defaultdict (list )
178
184
185
+ def _make_report (self : TimerProtocol ) -> TimerReport :
186
+ total_time = 0.0
187
+ for _ , durations in self .recorded_durations .items ():
188
+ array_value = np .array (durations )
189
+ array_sum = np .sum (array_value )
190
+ total_time += array_sum
191
+
192
+ action_stats = [
193
+ TimedActionStats (
194
+ action_name = a ,
195
+ mean_duration = np .mean (d ),
196
+ num_calls = len (d ),
197
+ total_duration = np .sum (d ),
198
+ percentage_of_total_time = 100.0 * np .sum (d ) / total_time ,
199
+ )
200
+ for a , d in self .recorded_durations .items ()
201
+ ]
202
+ action_stats .sort (reverse = True )
203
+ total_calls = sum (x .num_calls for x in action_stats )
204
+ return TimerReport (
205
+ timed_action_stats = action_stats ,
206
+ total_calls = total_calls ,
207
+ total_duration = total_time ,
208
+ )
209
+
179
210
180
211
class BoundedTimer (Timer ):
181
212
"""
@@ -215,32 +246,6 @@ def _apply_bounds(self, action_name: str) -> None:
215
246
)
216
247
217
248
218
- def _make_report (self : TimerProtocol ) -> TimerReport :
219
- total_time = 0.0
220
- for _ , durations in self .recorded_durations .items ():
221
- array_value = np .array (durations )
222
- array_sum = np .sum (array_value )
223
- total_time += array_sum
224
-
225
- action_stats = [
226
- TimedActionStats (
227
- action_name = a ,
228
- mean_duration = np .mean (d ),
229
- num_calls = len (d ),
230
- total_duration = np .sum (d ),
231
- percentage_of_total_time = 100.0 * np .sum (d ) / total_time ,
232
- )
233
- for a , d in self .recorded_durations .items ()
234
- ]
235
- action_stats .sort (reverse = True )
236
- total_calls = sum (x .num_calls for x in action_stats )
237
- return TimerReport (
238
- timed_action_stats = action_stats ,
239
- total_calls = total_calls ,
240
- total_duration = total_time ,
241
- )
242
-
243
-
244
249
def get_timer_summary (timer : TimerProtocol ) -> str :
245
250
"""Given a timer, generate a summary of all the recorded actions.
246
251
@@ -251,7 +256,7 @@ def get_timer_summary(timer: TimerProtocol) -> str:
251
256
ValueError
252
257
If the input Timer has no recorded actions
253
258
"""
254
- report : TimerReport = _make_report (timer )
259
+ report : TimerReport = timer . _make_report ()
255
260
256
261
sep : str = os .linesep
257
262
output_string = f"Timer Report{ sep } "
@@ -279,7 +284,6 @@ def log_row(action: str, mean: str, num_calls: str, total: str, per: str) -> str
279
284
output_string_len = len (header_string .expandtabs ()) - 1
280
285
sep_lines = f"{ sep } { '-' * output_string_len } "
281
286
output_string += sep_lines + header_string + sep_lines
282
-
283
287
output_string += log_row (
284
288
"Total" , "-" , f"{ report .total_calls :} " , f"{ report .total_duration :.5} " , "100 %"
285
289
)
0 commit comments