""" A module that helps to inject time profiling code in other modules. # Added class TimeProfiler - Anand 04/08/06 """ # Copyright 2008-2010 eGovMon # This program is distributed under the terms of the GNU General # Public License. # # This file is part of the eGovernment Monitoring # (eGovMon) # # eGovMon is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # eGovMon is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with eGovMon; if not, write to the Free Software # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, # MA 02110-1301 USA __author__ = "Anand B. Pillai" __updated__ = "$LastChangedDate$" __version__ = "0.3" import time def timeprofile(): """ A factory function to return an instance of TimeProfiler """ return TimeProfiler() class TimeProfiler: """ A utility class for profiling execution time for code """ def __init__(self): # Dictionary with times in seconds self.timedict = {} def mark(self, slot=''): """ Mark the current time into the slot 'slot' """ # Note: 'slot' has to be string type # we are not checking it here. self.timedict[slot] = time.time() def unmark(self, slot=''): """ Unmark the slot 'slot' """ # Note: 'slot' has to be string type # we are not checking it here. if self.timedict.has_key(slot): del self.timedict[slot] def lastdiff(self): """ Get time difference between now and the latest marked slot """ # To get the latest slot, just get the max of values return time.time() - max(self.timedict.values()) def elapsed(self, slot=''): """ Get the time difference between now and a previous time slot named 'slot' """ # Note: 'slot' has to be marked previously return time.time() - self.timedict.get(slot) def diff(self, slot1, slot2): """ Get the time difference between two marked time slots 'slot1' and 'slot2' """ return self.timedict.get(slot2) - self.timedict.get(slot1) def maxdiff(self): """ Return maximum time difference marked """ # Difference of max time with min time times = self.timedict.values() return max(times) - min(times) def timegap(self): """ Return the full time-gap since we started marking """ # Return now minus min times = self.timedict.values() return time.time() - min(times) def cleanup(self): """ Cleanup the dictionary of all marks """ self.timedict.clear() if __name__ == "__main__": # Demo code profiler = timeprofile() # Mark time profiler.mark() # Execute large loop for x in xrange(10000): pass # Get time print profiler.elapsed() # Do other things profiler.mark('t') for x in range(10000): for y in range(10000): pass print profiler.elapsed('t') # Get total time elapsed print profiler.timegap() # Get maximum diff for marks print profiler.maxdiff()