Last active
September 27, 2018 02:40
-
-
Save tilt-silvie/577cb78cf27886b6bcf792c9868eb4e6 to your computer and use it in GitHub Desktop.
Pythonによる定期処理(10ms)精度確認スクリプト
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import time | |
import numpy as np | |
from threading import Timer | |
class RepeatedTimer(Timer): | |
def __init__(self, interval, function, args=[], kwargs={}): | |
Timer.__init__(self, interval, self.run, args, kwargs) | |
self.thread = None | |
self.function = function | |
def run(self): | |
self.thread = Timer(self.interval, self.run) | |
self.thread.start() | |
self.function(*self.args, **self.kwargs) | |
def cancel(self): | |
if self.thread is not None: | |
self.thread.cancel() | |
self.thread.join() | |
del self.thread | |
class Log: | |
def __init__(self): | |
self.logs = [] | |
def add(self, data): | |
self.logs.append(data) | |
def get(self): | |
return self.logs | |
def func(logger): | |
logger.add(time.perf_counter()) | |
log = Log() | |
t = RepeatedTimer(0.01, func, [log]) | |
t.start() | |
time.sleep(1) | |
t.cancel() | |
diff = np.diff(log.get()) | |
print("mean:", np.mean(diff) * 1000, "[ms]") | |
print("median:", np.median(diff) * 1000, "[ms]") | |
print("stddev:", np.std(diff) * 1000, "[ms]") | |
print("---------------") | |
print(diff) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment