Skip to content

Commit

Permalink
use time.perf_counter instead of time.time
Browse files Browse the repository at this point in the history
  • Loading branch information
ebonnal committed Dec 28, 2024
1 parent 47ab6e5 commit c4ef670
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 15 deletions.
26 changes: 15 additions & 11 deletions streamable/iterators.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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


Expand All @@ -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)
Expand All @@ -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())
Expand Down Expand Up @@ -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",
)
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
8 changes: 4 additions & 4 deletions tests/test_readme.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down

0 comments on commit c4ef670

Please sign in to comment.