[Python-checkins] CVS: python/dist/src/Lib profile.py,1.40,1.41

Tim Peters tim_one@users.sourceforge.net
Tue, 09 Oct 2001 13:51:21 -0700


Update of /cvsroot/python/python/dist/src/Lib
In directory usw-pr-cvs1:/tmp/cvs-serv29499/python/Lib

Modified Files:
	profile.py 
Log Message:
Allow the profiler's calibration constant to be specified in the constructor
call, or via setting an instance or class vrbl.
Rewrote the calibration docs.
Modern boxes are so friggin' fast, and a profiler event does so much work
anyway, that the cost of looking up an instance vrbl (the bias constant)
per profile event just isn't a big deal.


Index: profile.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/profile.py,v
retrieving revision 1.40
retrieving revision 1.41
diff -C2 -d -r1.40 -r1.41
*** profile.py	2001/10/09 05:31:56	1.40
--- profile.py	2001/10/09 20:51:19	1.41
***************
*** 140,148 ****
      """
  
!     def __init__(self, timer=None):
          self.timings = {}
          self.cur = None
          self.cmd = ""
  
          if not timer:
              if os.name == 'mac':
--- 140,154 ----
      """
  
!     bias = 0  # calibration constant
! 
!     def __init__(self, timer=None, bias=None):
          self.timings = {}
          self.cur = None
          self.cmd = ""
  
+         if bias is None:
+             bias = self.bias
+         self.bias = bias     # Materialize in local dict for lookup speed.
+ 
          if not timer:
              if os.name == 'mac':
***************
*** 191,195 ****
          timer = self.timer
          t = timer()
!         t = t[0] + t[1] - self.t # - .00053 calibration constant
  
          if self.dispatch[event](self, frame,t):
--- 197,201 ----
          timer = self.timer
          t = timer()
!         t = t[0] + t[1] - self.t - self.bias
  
          if self.dispatch[event](self, frame,t):
***************
*** 199,204 ****
              r = timer()
              self.t = r[0] + r[1] - t # put back unrecorded delta
-         return
- 
  
      # Dispatch routine for best timer program (return = scalar, fastest if
--- 205,208 ----
***************
*** 207,216 ****
      def trace_dispatch_i(self, frame, event, arg):
          timer = self.timer
!         t = timer() - self.t # - 16e-6 # calibration constant
          if self.dispatch[event](self, frame,t):
              self.t = timer()
          else:
              self.t = timer() - t  # put back unrecorded delta
-         return
  
      # Dispatch routine for macintosh (timer returns time in ticks of
--- 211,219 ----
      def trace_dispatch_i(self, frame, event, arg):
          timer = self.timer
!         t = timer() - self.t - self.bias
          if self.dispatch[event](self, frame,t):
              self.t = timer()
          else:
              self.t = timer() - t  # put back unrecorded delta
  
      # Dispatch routine for macintosh (timer returns time in ticks of
***************
*** 219,229 ****
      def trace_dispatch_mac(self, frame, event, arg):
          timer = self.timer
!         t = timer()/60.0 - self.t # - 1 # calibration constant
!         if self.dispatch[event](self, frame,t):
              self.t = timer()/60.0
          else:
              self.t = timer()/60.0 - t  # put back unrecorded delta
-         return
- 
  
      # SLOW generic dispatch routine for timer returning lists of numbers
--- 222,230 ----
      def trace_dispatch_mac(self, frame, event, arg):
          timer = self.timer
!         t = timer()/60.0 - self.t - self.bias
!         if self.dispatch[event](self, frame, t):
              self.t = timer()/60.0
          else:
              self.t = timer()/60.0 - t  # put back unrecorded delta
  
      # SLOW generic dispatch routine for timer returning lists of numbers
***************
*** 231,241 ****
      def trace_dispatch_l(self, frame, event, arg):
          get_time = self.get_time
!         t = get_time() - self.t
  
!         if self.dispatch[event](self, frame,t):
              self.t = get_time()
          else:
              self.t = get_time() - t # put back unrecorded delta
-         return
  
      # In the event handlers, the first 3 elements of self.cur are unpacked
--- 232,241 ----
      def trace_dispatch_l(self, frame, event, arg):
          get_time = self.get_time
!         t = get_time() - self.t - self.bias
  
!         if self.dispatch[event](self, frame, t):
              self.t = get_time()
          else:
              self.t = get_time() - t # put back unrecorded delta
  
      # In the event handlers, the first 3 elements of self.cur are unpacked
***************
*** 431,437 ****
      # re-starts the stopwatch before the user's code really gets to
      # continue.  The following code tries to measure the difference on
!     # a per-event basis. The result can the be placed in the
!     # Profile.dispatch_event() routine for the given platform.  Note
!     # that this difference is only significant if there are a lot of
      # events, and relatively little user code per event.  For example,
      # code with small functions will typically benefit from having the
--- 431,437 ----
      # re-starts the stopwatch before the user's code really gets to
      # continue.  The following code tries to measure the difference on
!     # a per-event basis.
!     #
!     # Note that this difference is only significant if there are a lot of
      # events, and relatively little user code per event.  For example,
      # code with small functions will typically benefit from having the
***************
*** 462,471 ****
      # event/time ratio (i.e., the profiler would run slower, fur a very
      # low "value added" feature.)
-     #
-     # Plugging in the calibration constant doesn't slow down the
-     # profiler very much, and the accuracy goes way up.
      #**************************************************************
  
      def calibrate(self, m, verbose=0):
          get_time = self.get_time
  
--- 462,479 ----
      # event/time ratio (i.e., the profiler would run slower, fur a very
      # low "value added" feature.)
      #**************************************************************
  
      def calibrate(self, m, verbose=0):
+         if self.__class__ is not Profile:
+             raise TypeError("Subclasses must override .calibrate().")
+ 
+         saved_bias = self.bias
+         self.bias = 0
+         try:
+             return self._callibrate_inner(m, verbose)
+         finally:
+             self.bias = saved_bias
+ 
+     def _callibrate_inner(self, m, verbose):
          get_time = self.get_time