Skip to content

Commit c391956

Browse files
authored
Use time.perf_counter instead of time.monotonic (#114)
Use time.perf_counter instead of time.monotonic. Fixes 113
1 parent a229957 commit c391956

File tree

3 files changed

+10
-9
lines changed

3 files changed

+10
-9
lines changed

doc/source/index.rst

+1
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,5 @@ On top of that, some improvement makes v2.x preferable over v1.x
4747
- Sending data with a generator is now possible, accommodating use cases with large payloads
4848
- The module is fully type-hinted
4949
- It is possible to use a busy-wait to achieve even more precise timings. See the :ref:`wait_func parameter<param_wait_func>`
50+
- 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>`_
5051

isotp/protocol.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ def update(self) -> None:
259259
self.reset()
260260
return
261261

262-
t = time.monotonic()
262+
t = time.perf_counter()
263263

264264
while len(self.burst_time) > 0:
265265
t2 = self.burst_time[0]
@@ -283,7 +283,7 @@ def allowed_bytes(self) -> int:
283283
def inform_byte_sent(self, datalen: int) -> None:
284284
if self.enabled:
285285
bytelen = datalen * 8
286-
t = time.monotonic()
286+
t = time.perf_counter()
287287
self.bit_total += bytelen
288288
if len(self.burst_time) == 0:
289289
self.burst_time.append(t)
@@ -689,7 +689,7 @@ def load_params(self) -> None:
689689
def send(self,
690690
data: Union[bytes, bytearray, SendGenerator],
691691
target_address_type: Optional[Union[isotp.address.TargetAddressType, int]] = None,
692-
send_timeout: float = 0):
692+
send_timeout: Optional[float] = None):
693693
"""
694694
Enqueue an IsoTP frame to be sent over CAN network.
695695
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.
@@ -704,7 +704,7 @@ def send(self,
704704
:type target_address_type: int
705705
706706
:param send_timeout: Timeout value for blocking send. Unused if :ref:`blocking_send<param_blocking_send>` is ``False``
707-
:type send_timeout: float
707+
:type send_timeout: float or None
708708
709709
:raises ValueError: Given data is not a bytearray, a tuple (generator,size) or the size is too big
710710
:raises RuntimeError: Transmit queue is full
@@ -1583,9 +1583,9 @@ def _relay_thread_fn(self) -> None:
15831583
self.events.relay_thread_ready.set()
15841584
while not self.events.stop_requested.is_set():
15851585
rx_timeout = 0.0 if self.is_tx_throttled() else self.default_read_timeout
1586-
t1 = time.monotonic()
1586+
t1 = time.perf_counter()
15871587
data = self.user_rxfn(rx_timeout)
1588-
diff = time.monotonic() - t1
1588+
diff = time.perf_counter() - t1
15891589
if data is not None:
15901590
self.rx_relay_queue.put(data)
15911591
else: # No data received. Sleep if user is not blocking

isotp/tools.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,20 @@ def set_timeout(self, timeout: float) -> None:
2020
def start(self, timeout=None) -> None:
2121
if timeout is not None:
2222
self.set_timeout(timeout)
23-
self.start_time = time.monotonic_ns()
23+
self.start_time = time.perf_counter_ns()
2424

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

2828
def elapsed(self) -> float:
2929
if self.start_time is not None:
30-
return float(time.monotonic_ns() - self.start_time) / 1.0e9
30+
return float(time.perf_counter_ns() - self.start_time) / 1.0e9
3131
else:
3232
return 0
3333

3434
def elapsed_ns(self) -> int:
3535
if self.start_time is not None:
36-
return time.monotonic_ns() - self.start_time
36+
return time.perf_counter_ns() - self.start_time
3737
else:
3838
return 0
3939

0 commit comments

Comments
 (0)