I tend to record the running time of different snippets of code in a single function because this is a training system and repeatedly using one batch of data to test the running time is far from accurate enough. Thus I thing having a class (of anything that fits) to record the time could be super useful. Otherwise I have to get as many variables as the number of snippets I want to test speed for.
I see a great example in the following question. It's simple for testing one round for multiple snippets. However when it comes to average time, I don't think there is a easy fix to that (like adding a global variable). Is there any good solution to this? I think there should be something like every new instance with a same name keep their own variable space?
class CodeTimer:
def __init__(self, name=None):
self.name = " '" + name + "'" if name else ''
def __enter__(self):
self.start = timeit.default_timer()
def __exit__(self, exc_type, exc_value, traceback):
self.took = (timeit.default_timer() - self.start) * 1000.0
print('Code block' + self.name + ' took: ' + str(self.took) + ' ms')