Skip to content

Instantly share code, notes, and snippets.

@post2web
Created March 25, 2014 07:06
Show Gist options
  • Save post2web/9756499 to your computer and use it in GitHub Desktop.
Save post2web/9756499 to your computer and use it in GitHub Desktop.
#durty timing stuff around the code
import time
class Timer:
times = {}
@staticmethod
def start(key=''):
if key not in Timer.times:
Timer.times[key] = {'start': False, 'sum': 0}
assert Timer.times[key]['start'] == False
Timer.times[key]['start'] = time.time()
@staticmethod
def stop(key=''):
assert key in Timer.times
Timer.times[key]['sum'] += time.time() - Timer.times[key]['start']
Timer.times[key]['start'] = False
@staticmethod
def keys():
return Timer.times.keys()
@staticmethod
def sum(key=''):
assert key in Timer.times
assert Timer.times[key]['start'] == False
return Timer.times[key]['sum']
@staticmethod
def reset():
Timer.times = {}
@staticmethod
def report():
for key in Timer.keys():
print key, Timer.sum(key)
if __name__ == '__main__':
Timer.start()
Timer.stop()
Timer.start('foo')
time.sleep(2)
Timer.stop('foo')
Timer.start('foo')
time.sleep(1)
Timer.stop('foo')
Timer.start('aa')
time.sleep(1)
Timer.stop('aa')
Timer.report()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment