-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a loop timer to help work out where loops are show.
- Loading branch information
Showing
1 changed file
with
32 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
from collections import defaultdict | ||
|
||
import numpy as np | ||
import pandas as pd | ||
from tabulate import tabulate | ||
|
||
|
||
class LoopTimer: | ||
def __init__(self): | ||
self.timers = defaultdict(list) | ||
self.last_time = None | ||
self.curr_key = None | ||
|
||
def __call__(self, key): | ||
time = pd.Timestamp.now() | ||
if self.curr_key is not None: | ||
self.timers[(self.curr_key, key)].append(time - self.last_time) | ||
|
||
self.last_time = time | ||
self.curr_key = key | ||
|
||
def __str__(self): | ||
output = [] | ||
|
||
for k in self.timers.keys(): | ||
k1, k2 = k | ||
timers = self.timers[k] | ||
times_ms = [t.microseconds for t in timers] | ||
time_mean_ms = np.mean(times_ms) | ||
time_std_ms = np.std(times_ms) | ||
output.append((f'{k1} -> {k2}', f'{time_mean_ms / 1e6:.2g}s', f'(+/- {time_std_ms / 1e6:.2g}s)')) | ||
return tabulate(output, headers=('tx', 'mean', 'std')) |