home *** CD-ROM | disk | FTP | other *** search
- <TITLE>Calibration -- Python library reference</TITLE>
- Next: <A HREF="../p/profiler_extensions" TYPE="Next">Profiler Extensions</A>
- Prev: <A HREF="../l/limitations" TYPE="Prev">Limitations</A>
- Up: <A HREF="../t/the_python_profiler" TYPE="Up">The Python Profiler</A>
- Top: <A HREF="../t/top" TYPE="Top">Top</A>
- <H1>9.7. Calibration</H1>
- The profiler class has a hard coded constant that is added to each
- event handling time to compensate for the overhead of calling the time
- function, and socking away the results. The following procedure can
- be used to obtain this constant for a given platform (see discussion
- in section Limitations above).
- <P>
- <UL COMPACT><CODE> import profile<P>
- pr = profile.Profile()<P>
- pr.calibrate(100)<P>
- pr.calibrate(100)<P>
- pr.calibrate(100)<P>
- </CODE></UL>
- The argument to calibrate() is the number of times to try to do the
- sample calls to get the CPU times. If your computer is <I>very</I>
- fast, you might have to do:
- <P>
- <UL COMPACT><CODE> pr.calibrate(1000)<P>
- </CODE></UL>
- or even:
- <P>
- <UL COMPACT><CODE> pr.calibrate(10000)<P>
- </CODE></UL>
- The object of this exercise is to get a fairly consistent result.
- When you have a consistent answer, you are ready to use that number in
- the source code. For a Sun Sparcstation 1000 running Solaris 2.3, the
- magical number is about .00053. If you have a choice, you are better
- off with a smaller constant, and your results will ``less often'' show
- up as negative in profile statistics.
- <P>
- The following shows how the trace_dispatch() method in the Profile
- class should be modified to install the calibration constant on a Sun
- Sparcstation 1000:
- <P>
- <UL COMPACT><CODE> def trace_dispatch(self, frame, event, arg):<P>
- t = self.timer()<P>
- t = t[0] + t[1] - self.t - .00053 # Calibration constant<P>
- <P>
- if self.dispatch[event](frame,t):<P>
- t = self.timer()<P>
- self.t = t[0] + t[1]<P>
- else:<P>
- r = self.timer()<P>
- self.t = r[0] + r[1] - t # put back unrecorded delta<P>
- return<P>
- </CODE></UL>
- Note that if there is no calibration constant, then the line
- containing the callibration constant should simply say:
- <P>
- <UL COMPACT><CODE> t = t[0] + t[1] - self.t # no calibration constant<P>
- </CODE></UL>
- You can also achieve the same results using a derived class (and the
- profiler will actually run equally fast!!), but the above method is
- the simplest to use. I could have made the profiler ``self
- calibrating'', but it would have made the initialization of the
- profiler class slower, and would have required some <I>very</I> fancy
- coding, or else the use of a variable where the constant `<SAMP>.00053</SAMP>'
- was placed in the code shown. This is a <B>VERY</B> critical
- performance section, and there is no reason to use a variable lookup
- at this point, when a constant can be used.
- <P>
-