Skip to content

Instantly share code, notes, and snippets.

@badgateway666
Last active December 23, 2019 23:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save badgateway666/8119968baf6d09223da2be1aa885b9e9 to your computer and use it in GitHub Desktop.
Save badgateway666/8119968baf6d09223da2be1aa885b9e9 to your computer and use it in GitHub Desktop.
Timer which updates average delay between measurements incrementally
import time
class Incremental_Timer(object):
def __init__(self):
self.start_time = time.time()
self.count_measurement = 0
self.current_avg = 0
self.prev_time = None
self.current_time = None
self.initialized = False
def init_timing(self):
if self.initialized:
print("This Timer was already initialized.")
return
self.prev_time = time.time()
def update(self):
if not self.initialized:
print("This timer is not initialized yet.")
return
self.current_time = time.time()
self.count_measurement += 1
self.current_avg = self.current_avg + (((self.current_time - self.prev_time) - self.current_avg) / self.count_measurement)
return self.current_avg
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment