Skip to content

Commit

Permalink
use time.perf_counter() instead of time.clock(), close #92
Browse files Browse the repository at this point in the history
  • Loading branch information
NiklasRosenstein committed Sep 4, 2021
1 parent dd69a58 commit b105d03
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
2 changes: 1 addition & 1 deletion examples/04_emg_rate.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def on_arm_synced(self, event):
event.device.stream_emg(True)

def on_emg(self, event):
t = time.clock()
t = time.perf_counter()
if self.last_time is not None:
self.times.append(t - self.last_time)
if len(self.times) > self.n:
Expand Down
13 changes: 7 additions & 6 deletions myo/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,11 @@ class TimeInterval(object):
A helper class to keep track of a time interval.
"""

def __init__(self, value, value_on_reset=None):
def __init__(self, value, value_on_reset=None, clock=None):
self.value = value
self.value_on_reset = value_on_reset
self.start = time.clock()
self.clock = clock or time.perf_counter
self.start = self.clock

def check(self):
"""
Expand All @@ -41,15 +42,15 @@ def check(self):

if self.value is None:
return True
return (time.clock() - self.start) >= self.value
return (self.clock() - self.start) >= self.value

def reset(self, value=None):
"""
Resets the start time of the interval to now or the specified value.
"""

if value is None:
value = time.clock()
value = self.clock()
self.start = value
if self.value_on_reset:
self.value = self.value_on_reset
Expand All @@ -74,7 +75,7 @@ def check(self):

if self.value is None:
return False
return (time.clock() - self.start) >= self.value
return (self.clock() - self.start) >= self.value

def remainder(self, max_value=None):
"""
Expand All @@ -84,7 +85,7 @@ def remainder(self, max_value=None):

if self.value is None:
return max_value
remainder = self.value - (time.clock() - self.start)
remainder = self.value - (self.clock() - self.start)
if remainder < 0.0:
return 0.0
elif max_value is not None and remainder > max_value:
Expand Down

0 comments on commit b105d03

Please sign in to comment.