Skip to content

Commit

Permalink
Update timer
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikGartner committed Mar 2, 2019
1 parent c34a5ac commit 1c40186
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 11 deletions.
2 changes: 1 addition & 1 deletion solutions/strategies/default.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import collections
import algorithms

from ..utils import parse_in, write_ans, dprint, progressbar
from ..utils import parse_in, write_ans, dprint, progressbar, timethis
from .utils import *


Expand Down
36 changes: 26 additions & 10 deletions solutions/utils/status.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from tqdm import tqdm
from takethetime import ttt
import time

DEBUG = False

Expand All @@ -19,12 +19,28 @@ def progressbar(itr):


def timethis(name):
if DEBUG:
return ttt(name=name)
else:
class controlled_execution:
def __enter__(self):
pass
def __exit__(self, type, value, traceback):
pass
return controlled_execution()
"""
Usage:
with timethis('Sorting loop') as t:
do_func()
t.info('Nbr items: %d' % len(stuff))
"""
class timewrapper:
def __init__(self, name, debug):
self.name = name
self.messages = []
self.debug = debug

def info(self, msg):
self.messages.append(msg)

def __enter__(self):
self.t0 = time.time()
return self

def __exit__(self, type, value, traceback):
if self.debug:
print('{}: {:0.5f}s'.format(self.name, time.time() - self.t0))
for msg in self.messages:
print('- {}'.format(msg))
return timewrapper(name, DEBUG)

0 comments on commit 1c40186

Please sign in to comment.