Last active
December 23, 2019 23:54
-
-
Save badgateway666/8119968baf6d09223da2be1aa885b9e9 to your computer and use it in GitHub Desktop.
Timer which updates average delay between measurements incrementally
This file contains 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
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