Skip to content

Commit

Permalink
Prettier formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
cpcloud committed Jun 7, 2015
1 parent 43ced73 commit e9a0f80
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions autotime.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,36 @@


class LineWatcher(object):
def __init__(self, ip):
self.shell = ip
def __init__(self):
self.start_time = 0.0

def start(self):
self.start_time = time.time()

def stop(self):
print('time: %.6f s' % (time.time() - self.start_time))
diff = time.time() - self.start_time
assert diff > 0
print('time: %s' % format_delta(diff))


def format_delta(num, suffix='s', threshold=1000):
base_unit = 1000000000
num *= base_unit # smallest unit is 1 / 1000000000 of the biggest unit
for unit in ['n', 'u', 'm', '']:
if num < threshold:
return '%3.2f %s%s' % (num, unit, suffix)
num /= threshold
return '%.2f %s' % (num, suffix)


timer = LineWatcher()


def load_ipython_extension(ip):
timer = LineWatcher(ip)
ip.events.register('pre_run_cell', timer.start)
ip.events.register('post_run_cell', timer.stop)


def unload_ipython_extension(ip):
timer = LineWatcher(ip)
ip.events.unregister('pre_run_cell', timer.start)
ip.events.unregister('post_run_cell', timer.stop)

0 comments on commit e9a0f80

Please sign in to comment.