Update avg/eta/eta_td less frequently

When the progress bar is updated frequently (every few milliseconds),
the eta can change so quickly that it's impossible to read.

This commit updates the average while sma_window is being filled, then
after every second.

This means we're calling monotonic/time often, but those calls take less
than 100 nsec per loop on Linux, Windows and macOS [0], which is
equivalent to one attribute lookup or two.

[0]: https://github.com/python-trio/trio/issues/33
This commit is contained in:
Quentin Pradet
2018-11-16 16:52:10 +04:00
parent 326413a271
commit c1e07104e1

View File

@@ -40,6 +40,7 @@ class Infinite(object):
self.index = 0
self.start_ts = monotonic()
self.avg = 0
self._avg_update_ts = self.start_ts
self._ts = self.start_ts
self._xput = deque(maxlen=self.sma_window)
for key, val in kwargs.items():
@@ -69,8 +70,14 @@ class Infinite(object):
def update_avg(self, n, dt):
if n > 0:
xput_len = len(self._xput)
self._xput.append(dt / n)
now = monotonic()
# update when we're still filling _xput, then after every second
if (xput_len < self.sma_window or
now - self._avg_update_ts > 1):
self.avg = sum(self._xput) / len(self._xput)
self._avg_update_ts = now
def update(self):
pass