home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 5 / DATAFILE_PDCD5.iso / utilities / p / python / pyhtmldoc / c / calibratio next >
Text File  |  1996-11-14  |  3KB  |  67 lines

  1. <TITLE>Calibration -- Python library reference</TITLE>
  2. Next: <A HREF="../p/profiler_extensions" TYPE="Next">Profiler Extensions</A>  
  3. Prev: <A HREF="../l/limitations" TYPE="Prev">Limitations</A>  
  4. Up: <A HREF="../t/the_python_profiler" TYPE="Up">The Python Profiler</A>  
  5. Top: <A HREF="../t/top" TYPE="Top">Top</A>  
  6. <H1>9.7. Calibration</H1>
  7. The profiler class has a hard coded constant that is added to each
  8. event handling time to compensate for the overhead of calling the time
  9. function, and socking away the results.  The following procedure can
  10. be used to obtain this constant for a given platform (see discussion
  11. in section Limitations above).
  12. <P>
  13. <UL COMPACT><CODE>    import profile<P>
  14.     pr = profile.Profile()<P>
  15.     pr.calibrate(100)<P>
  16.     pr.calibrate(100)<P>
  17.     pr.calibrate(100)<P>
  18. </CODE></UL>
  19. The argument to calibrate() is the number of times to try to do the
  20. sample calls to get the CPU times.  If your computer is <I>very</I>
  21. fast, you might have to do:
  22. <P>
  23. <UL COMPACT><CODE>    pr.calibrate(1000)<P>
  24. </CODE></UL>
  25. or even:
  26. <P>
  27. <UL COMPACT><CODE>    pr.calibrate(10000)<P>
  28. </CODE></UL>
  29. The object of this exercise is to get a fairly consistent result.
  30. When you have a consistent answer, you are ready to use that number in
  31. the source code.  For a Sun Sparcstation 1000 running Solaris 2.3, the
  32. magical number is about .00053.  If you have a choice, you are better
  33. off with a smaller constant, and your results will ``less often'' show
  34. up as negative in profile statistics.
  35. <P>
  36. The following shows how the trace_dispatch() method in the Profile
  37. class should be modified to install the calibration constant on a Sun
  38. Sparcstation 1000:
  39. <P>
  40. <UL COMPACT><CODE>    def trace_dispatch(self, frame, event, arg):<P>
  41.         t = self.timer()<P>
  42.         t = t[0] + t[1] - self.t - .00053 # Calibration constant<P>
  43. <P>
  44.         if self.dispatch[event](frame,t):<P>
  45.             t = self.timer()<P>
  46.             self.t = t[0] + t[1]<P>
  47.         else:<P>
  48.             r = self.timer()<P>
  49.             self.t = r[0] + r[1] - t # put back unrecorded delta<P>
  50.         return<P>
  51. </CODE></UL>
  52. Note that if there is no calibration constant, then the line
  53. containing the callibration constant should simply say:
  54. <P>
  55. <UL COMPACT><CODE>        t = t[0] + t[1] - self.t  # no calibration constant<P>
  56. </CODE></UL>
  57. You can also achieve the same results using a derived class (and the
  58. profiler will actually run equally fast!!), but the above method is
  59. the simplest to use.  I could have made the profiler ``self
  60. calibrating'', but it would have made the initialization of the
  61. profiler class slower, and would have required some <I>very</I> fancy
  62. coding, or else the use of a variable where the constant `<SAMP>.00053</SAMP>'
  63. was placed in the code shown.  This is a <B>VERY</B> critical
  64. performance section, and there is no reason to use a variable lookup
  65. at this point, when a constant can be used.
  66. <P>
  67.