Skip to content

Instantly share code, notes, and snippets.

@kapulkin
Last active October 12, 2020 21:38
Show Gist options
  • Save kapulkin/4ac34eadb566274231cd977aafe011ae to your computer and use it in GitHub Desktop.
Save kapulkin/4ac34eadb566274231cd977aafe011ae to your computer and use it in GitHub Desktop.
Measuring evaluation time in python
import time
import logging
logger = logging.getLogger(__name__)
class Timer:
def __init__(self, name: str, maxCount = None):
self.name = name
self.time = 0
self.count = 0
self.maxCount = maxCount
def start(self):
self.startTime = time.time()
if self.maxCount is not None:
self.time = 0
self.count = 0
def end(self):
self.time += time.time() - self.startTime
self.count += 1
def append(self):
self.time += time.time() - self.startTime
def after_append(self):
self.count += 1
def averageTime(self):
return self.time / self.count if self.count > 0 else -1
def printAverageTime(self):
print('{} time {}'.format(self.name, self.averageTime()))
def logAverageTime(self):
logger.debug('%s time %s', str(self.name), str(self.averageTime()))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment