Skip to content

Instantly share code, notes, and snippets.

@tilt-silvie
Last active September 27, 2018 02:40
Show Gist options
  • Save tilt-silvie/577cb78cf27886b6bcf792c9868eb4e6 to your computer and use it in GitHub Desktop.
Save tilt-silvie/577cb78cf27886b6bcf792c9868eb4e6 to your computer and use it in GitHub Desktop.
Pythonによる定期処理(10ms)精度確認スクリプト
#!/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