diff --git a/streamable/iterators.py b/streamable/iterators.py index 712d5f6..b6f1661 100644 --- a/streamable/iterators.py +++ b/streamable/iterators.py @@ -192,11 +192,13 @@ def __init__( self._current_group: List[T] = [] def _interval_seconds_have_elapsed(self) -> bool: - return (time.time() - self._last_group_yielded_at) >= self._interval_seconds + return ( + time.perf_counter() - self._last_group_yielded_at + ) >= self._interval_seconds def __next__(self) -> List[T]: if not self._last_group_yielded_at: - self._last_group_yielded_at = time.time() + self._last_group_yielded_at = time.perf_counter() if self._to_be_raised: e, self._to_be_raised = self._to_be_raised, None raise e @@ -211,7 +213,7 @@ def __next__(self) -> List[T]: self._to_be_raised = e group, self._current_group = self._current_group, [] - self._last_group_yielded_at = time.time() + self._last_group_yielded_at = time.perf_counter() return group @@ -229,7 +231,9 @@ def __init__( self._groups_by: DefaultDict[U, List[T]] = defaultdict(list) def _interval_seconds_have_elapsed(self) -> bool: - return (time.time() - self._last_group_yielded_at) >= self._interval_seconds + return ( + time.perf_counter() - self._last_group_yielded_at + ) >= self._interval_seconds def _group_next_elem(self) -> None: elem = next(self.iterator) @@ -255,12 +259,12 @@ def _pop_largest_group(self) -> Tuple[U, List[T]]: return largest_group_key, self._groups_by.pop(largest_group_key) def _return_group(self, group: Tuple[U, List[T]]) -> Tuple[U, List[T]]: - self._last_group_yielded_at = time.time() + self._last_group_yielded_at = time.perf_counter() return group def __next__(self) -> Tuple[U, List[T]]: if not self._last_group_yielded_at: - self._last_group_yielded_at = time.time() + self._last_group_yielded_at = time.perf_counter() if self._is_exhausted: if self._groups_by: return self._return_group(self._pop_first_group()) @@ -351,12 +355,12 @@ def __init__(self, iterator: Iterator[T], what: str, base: int = 2) -> None: self._n_yields = 0 self._n_errors = 0 self._logged_n_calls = 0 - self._start_time = time.time() + self._start_time = time.perf_counter() def _log(self) -> None: get_logger().info( "[%s %s] %s", - f"duration={datetime.datetime.fromtimestamp(time.time()) - datetime.datetime.fromtimestamp(self._start_time)}", + f"duration={datetime.datetime.fromtimestamp(time.perf_counter()) - datetime.datetime.fromtimestamp(self._start_time)}", f"errors={self._n_errors}", f"{self._n_yields} {self.what} yielded", ) @@ -407,10 +411,10 @@ def __init__(self, iterator: Iterator[T], interval: datetime.timedelta) -> None: def __next__(self) -> T: elem, catched_error = self.safe_next() if self._last_yield_at: - elapsed_time = time.time() - self._last_yield_at + elapsed_time = time.perf_counter() - self._last_yield_at if elapsed_time < self._interval_seconds: time.sleep(self._interval_seconds - elapsed_time) - self._last_yield_at = time.time() + self._last_yield_at = time.perf_counter() if catched_error: raise catched_error @@ -436,7 +440,7 @@ def __init__( def __next__(self) -> T: elem, catched_error = self.safe_next() - now = time.time() + now = time.perf_counter() if not self._offset: self._offset = now now -= self._offset diff --git a/tests/test_readme.py b/tests/test_readme.py index aa69fe8..4957b09 100644 --- a/tests/test_readme.py +++ b/tests/test_readme.py @@ -105,10 +105,10 @@ def test_throttle_example(self) -> None: integers_5_per_sec: Stream[int] = integers.throttle(per_second=3) - start = time.time() + start = time.perf_counter() # takes 3s: ceil(10 integers / 3 per_second) - 1 assert list(integers_5_per_sec) == [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] - assert 2.99 < time.time() - start < 3.25 + assert 2.99 < time.perf_counter() - start < 3.25 from datetime import timedelta @@ -117,10 +117,10 @@ def test_throttle_example(self) -> None: .throttle(interval=timedelta(milliseconds=100)) ) - start = time.time() + start = time.perf_counter() # takes 900 millis: (10 integers - 1) * 100 millis assert list(integers_every_100_millis) == [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] - assert 0.89 < time.time() - start < 0.95 + assert 0.89 < time.perf_counter() - start < 0.95 def test_group_example(self) -> None: global integers_by_parity