Pella86

Timing function

Jun 29th, 2017
259
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.56 KB | None | 0 0
  1. import time
  2.  
  3. # Timing utils
  4.  
  5. class TimingsTot:
  6.     def __init__(self, path = None, title = "insert date time here", debug_mode = True):
  7.         self.starttime = time.perf_counter()
  8.         self.nowtime = time.perf_counter()
  9.         self.lastcall = time.perf_counter()
  10.        
  11.         self.logfilepath = path
  12.         self.debug_mode = debug_mode
  13.        
  14.         with open(self.logfilepath, 'a') as f:
  15.             f.write("--------"+ title + "---------\n")
  16.    
  17.     def convert_in_ddhhss(self, seconds):
  18.         hh = 0
  19.         mm = 0
  20.         ss = 0
  21.         mm, ss = divmod(int(seconds), 60)
  22.         hh, mm = divmod(mm, 60)    
  23.        
  24.         return "{0:0>2}:{1:0>2}:{2:0>2}".format(hh, mm, ss)
  25.    
  26.     def gettimestr(self):
  27.         self.nowtime = time.perf_counter()
  28.         subtime =  self.nowtime - self.lastcall
  29.         subtime = self.convert_in_ddhhss(subtime)
  30.         s  = "Elapsed time for subprocess: {0}\n".format(subtime)
  31.        
  32.         totaltime = self.nowtime - self.starttime
  33.         totaltime = self.convert_in_ddhhss(totaltime)
  34.         s += "Total elapsed time: {0}".format(totaltime)
  35.        
  36.         self.lastcall = time.perf_counter()
  37.         return s
  38.    
  39.     def logtimestr(self, title):
  40.         s = self.gettimestr()
  41.         if self.logfilepath != None:
  42.             with open(self.logfilepath, 'a') as f:
  43.                 f.write(title + '\n')
  44.                 f.write(s + '\n')
  45.         if self.debug_mode:
  46.             print(title)
  47.             print(s)
  48.    
  49.     def __str__(self):
  50.         s = 'Not available'
  51.         return s
Advertisement
Add Comment
Please, Sign In to add comment