Skip to content

Commit

Permalink
Use time.perf_counter instead of time.monotonic (#114)
Browse files Browse the repository at this point in the history
Use time.perf_counter instead of time.monotonic. Fixes 113
  • Loading branch information
pylessard authored Jan 23, 2024
1 parent a229957 commit c391956
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 9 deletions.
1 change: 1 addition & 0 deletions doc/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,5 @@ On top of that, some improvement makes v2.x preferable over v1.x
- Sending data with a generator is now possible, accommodating use cases with large payloads
- The module is fully type-hinted
- It is possible to use a busy-wait to achieve even more precise timings. See the :ref:`wait_func parameter<param_wait_func>`
- Performances on Windows are greatly improved by the usage of ``time.perf_counter`` instead of ``time.monotonic``. See `issue #113 <https://github.com/pylessard/python-can-isotp/issues/113>`_

12 changes: 6 additions & 6 deletions isotp/protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ def update(self) -> None:
self.reset()
return

t = time.monotonic()
t = time.perf_counter()

while len(self.burst_time) > 0:
t2 = self.burst_time[0]
Expand All @@ -283,7 +283,7 @@ def allowed_bytes(self) -> int:
def inform_byte_sent(self, datalen: int) -> None:
if self.enabled:
bytelen = datalen * 8
t = time.monotonic()
t = time.perf_counter()
self.bit_total += bytelen
if len(self.burst_time) == 0:
self.burst_time.append(t)
Expand Down Expand Up @@ -689,7 +689,7 @@ def load_params(self) -> None:
def send(self,
data: Union[bytes, bytearray, SendGenerator],
target_address_type: Optional[Union[isotp.address.TargetAddressType, int]] = None,
send_timeout: float = 0):
send_timeout: Optional[float] = None):
"""
Enqueue an IsoTP frame to be sent over CAN network.
When performing a blocking send, this method returns only when the transmission is complete or raise an exception when a failure or a timeout occurs.
Expand All @@ -704,7 +704,7 @@ def send(self,
:type target_address_type: int
:param send_timeout: Timeout value for blocking send. Unused if :ref:`blocking_send<param_blocking_send>` is ``False``
:type send_timeout: float
:type send_timeout: float or None
:raises ValueError: Given data is not a bytearray, a tuple (generator,size) or the size is too big
:raises RuntimeError: Transmit queue is full
Expand Down Expand Up @@ -1583,9 +1583,9 @@ def _relay_thread_fn(self) -> None:
self.events.relay_thread_ready.set()
while not self.events.stop_requested.is_set():
rx_timeout = 0.0 if self.is_tx_throttled() else self.default_read_timeout
t1 = time.monotonic()
t1 = time.perf_counter()
data = self.user_rxfn(rx_timeout)
diff = time.monotonic() - t1
diff = time.perf_counter() - t1
if data is not None:
self.rx_relay_queue.put(data)
else: # No data received. Sleep if user is not blocking
Expand Down
6 changes: 3 additions & 3 deletions isotp/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,20 @@ def set_timeout(self, timeout: float) -> None:
def start(self, timeout=None) -> None:
if timeout is not None:
self.set_timeout(timeout)
self.start_time = time.monotonic_ns()
self.start_time = time.perf_counter_ns()

def stop(self) -> None:
self.start_time = None

def elapsed(self) -> float:
if self.start_time is not None:
return float(time.monotonic_ns() - self.start_time) / 1.0e9
return float(time.perf_counter_ns() - self.start_time) / 1.0e9
else:
return 0

def elapsed_ns(self) -> int:
if self.start_time is not None:
return time.monotonic_ns() - self.start_time
return time.perf_counter_ns() - self.start_time
else:
return 0

Expand Down

0 comments on commit c391956

Please sign in to comment.