''' Created on Sep 21, 2009 @author: tcezard This module enable its user to monitor the amount of time spend in between two commands start and stop. The module is fairly imprecise if the monitored task is quick as the start and stop commands are fairly slow (2e-07 - 5e-07) ''' from time import time from math import sqrt import logging total = {} started = {} def start(key): started[key]=time() def stop(key): stop=time() start=started.pop(key,None) if start: if total.has_key(key): total[key].append(stop-float(start)) else: total[key]=[stop-float(start)] else: logging.error("stopping non started timer: %s"%key) def print_all(): header = [ 'title', 'N', 'sum (s)', 'min (s)', 'max (s)', 'mean (s)','std dev (s)' ] line_array=[] for key in total.keys(): values=total.get(key) n = len(values) s=sum(values) mean = s / n sd = sqrt(sum((x-mean)**2 for x in values) / n) line_array.append([key, n,s,min(values),max(values),mean,sd]) #logging.info("%s: n=%s\tsum=%s\tmin=%s\tmax=%s\tmean=%s\tstd_dev=%s"%(key,n,s,min(values),max(values),mean,sd)) print format_array_line(line_array, header) def format_array_line(line_array, column_header): """This function creates a array formated string of all the job provided.""" #get the column length column_length=[] for header in column_header: column_length.append(len(header)) for sp_line in line_array: for pos in range(len(sp_line)): column_length[pos] length = len(str(sp_line[pos])) if column_length[pos]