Source code for ClearMap.Utils.Timer
# -*- coding: utf-8 -*-
"""
Provides tools for timing
"""
#:copyright: Copyright 2015 by Christoph Kirst, The Rockefeller University, New York City
#:license: GNU, see LICENSE.txt for details.
import time
[docs]class Timer(object):
"""Class to stop time and print results in formatted way
Attributes:
time (float): the time since the timer was started
"""
def __init__(self, verbose=False):
self.verbose = verbose;
self.start();
[docs] def start(self):
"""Start the timer"""
self.time = time.time();
[docs] def reset(self):
"""Reset the timer"""
self.time = time.time();
[docs] def elapsedTime(self, head = None, asstring = True):
"""Calculate elapsed time and return as formated string
Arguments:
head (str or None): prefix to the string
asstring (bool): return as string or float
Returns:
str or float: elapsed time
"""
t = time.time();
if asstring:
t = self.formatElapsedTime(t - self.time);
if head != None:
return head + ": elapsed time: " + t;
else:
return "Elapsed time: " + t;
else:
return t - self.time;
[docs] def printElapsedTime(self, head = None):
"""Print elapsed time as formated string
Arguments:
head (str or None): prefix to the string
"""
print self.elapsedTime(head = head);