⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 profile-calibration.html

📁 一本很好的python的说明书,适合对python感兴趣的人
💻 HTML
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>10.7 Calibration </title>
<META NAME="description" CONTENT="10.7 Calibration ">
<META NAME="keywords" CONTENT="lib">
<META NAME="resource-type" CONTENT="document">
<META NAME="distribution" CONTENT="global">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link rel="STYLESHEET" href="lib.css" tppabs="http://www.python.org/doc/current/lib/lib.css">
<LINK REL="next" href="Profiler_Extensions.html" tppabs="http://www.python.org/doc/current/lib/Profiler_Extensions.html">
<LINK REL="previous" href="profile-limits.html" tppabs="http://www.python.org/doc/current/lib/profile-limits.html">
<LINK REL="up" href="profile.html" tppabs="http://www.python.org/doc/current/lib/profile.html">
<LINK REL="next" href="Profiler_Extensions.html" tppabs="http://www.python.org/doc/current/lib/Profiler_Extensions.html">
</head>
<body>
<DIV CLASS="navigation"><table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td><A href="profile-limits.html" tppabs="http://www.python.org/doc/current/lib/profile-limits.html"><img src="previous.gif" tppabs="http://www.python.org/doc/current/icons/previous.gif" border="0" height="32"
  alt="Previous Page" width="32"></A></td>
<td><A href="profile.html" tppabs="http://www.python.org/doc/current/lib/profile.html"><img src="up.gif" tppabs="http://www.python.org/doc/current/icons/up.gif" border="0" height="32"
  alt="Up One Level" width="32"></A></td>
<td><A href="Profiler_Extensions.html" tppabs="http://www.python.org/doc/current/lib/Profiler_Extensions.html"><img src="next.gif" tppabs="http://www.python.org/doc/current/icons/next.gif" border="0" height="32"
  alt="Next Page" width="32"></A></td>
<td align="center" width="100%">Python Library Reference</td>
<td><A href="contents.html" tppabs="http://www.python.org/doc/current/lib/contents.html"><img src="contents.gif" tppabs="http://www.python.org/doc/current/icons/contents.gif" border="0" height="32"
  alt="Contents" width="32"></A></td>
<td><a href="modindex.html" tppabs="http://www.python.org/doc/current/lib/modindex.html" title="Module Index"><img src="modules.gif" tppabs="http://www.python.org/doc/current/icons/modules.gif" border="0" height="32"
  alt="Module Index" width="32"></a></td>
<td><A href="genindex.html" tppabs="http://www.python.org/doc/current/lib/genindex.html"><img src="index.gif" tppabs="http://www.python.org/doc/current/icons/index.gif" border="0" height="32"
  alt="Index" width="32"></A></td>
</tr></table>
<b class="navlabel">Previous:</b> <a class="sectref" href="profile-limits.html" tppabs="http://www.python.org/doc/current/lib/profile-limits.html">10.6 Limitations</A>
<b class="navlabel">Up:</b> <a class="sectref" href="profile.html" tppabs="http://www.python.org/doc/current/lib/profile.html">10. The Python Profiler</A>
<b class="navlabel">Next:</b> <a class="sectref" href="Profiler_Extensions.html" tppabs="http://www.python.org/doc/current/lib/Profiler_Extensions.html">10.8 Extensions  </A>
<br><hr></DIV>
<!--End of Navigation Panel-->

<H1>
<BR>
10.7 Calibration 
</H1>

<P>
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>
<dl><dd><pre class="verbatim">
import profile
pr = profile.Profile()
print pr.calibrate(100)
print pr.calibrate(100)
print pr.calibrate(100)
</pre></dl>

<P>
The argument to <tt class="method">calibrate()</tt> 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>
<dl><dd><pre class="verbatim">
pr.calibrate(1000)
</pre></dl>

<P>
or even:

<P>
<dl><dd><pre class="verbatim">
pr.calibrate(10000)
</pre></dl>

<P>
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>
<dl><dd><pre class="verbatim">
def trace_dispatch(self, frame, event, arg):
    t = self.timer()
    t = t[0] + t[1] - self.t - .00053 # Calibration constant

    if self.dispatch[event](frame,t):
        t = self.timer()
        self.t = t[0] + t[1]
    else:
        r = self.timer()
        self.t = r[0] + r[1] - t # put back unrecorded delta
    return
</pre></dl>

<P>
Note that if there is no calibration constant, then the line
containing the callibration constant should simply say:

<P>
<dl><dd><pre class="verbatim">
t = t[0] + t[1] - self.t  # no calibration constant
</pre></dl>

<P>
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 "<tt class="samp">.00053</tt>"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>

<DIV CLASS="navigation"><p><hr><table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td><A href="profile-limits.html" tppabs="http://www.python.org/doc/current/lib/profile-limits.html"><img src="previous.gif" tppabs="http://www.python.org/doc/current/icons/previous.gif" border="0" height="32"
  alt="Previous Page" width="32"></A></td>
<td><A href="profile.html" tppabs="http://www.python.org/doc/current/lib/profile.html"><img src="up.gif" tppabs="http://www.python.org/doc/current/icons/up.gif" border="0" height="32"
  alt="Up One Level" width="32"></A></td>
<td><A href="Profiler_Extensions.html" tppabs="http://www.python.org/doc/current/lib/Profiler_Extensions.html"><img src="next.gif" tppabs="http://www.python.org/doc/current/icons/next.gif" border="0" height="32"
  alt="Next Page" width="32"></A></td>
<td align="center" width="100%">Python Library Reference</td>
<td><A href="contents.html" tppabs="http://www.python.org/doc/current/lib/contents.html"><img src="contents.gif" tppabs="http://www.python.org/doc/current/icons/contents.gif" border="0" height="32"
  alt="Contents" width="32"></A></td>
<td><a href="modindex.html" tppabs="http://www.python.org/doc/current/lib/modindex.html" title="Module Index"><img src="modules.gif" tppabs="http://www.python.org/doc/current/icons/modules.gif" border="0" height="32"
  alt="Module Index" width="32"></a></td>
<td><A href="genindex.html" tppabs="http://www.python.org/doc/current/lib/genindex.html"><img src="index.gif" tppabs="http://www.python.org/doc/current/icons/index.gif" border="0" height="32"
  alt="Index" width="32"></A></td>
</tr></table>
<b class="navlabel">Previous:</b> <a class="sectref" href="profile-limits.html" tppabs="http://www.python.org/doc/current/lib/profile-limits.html">10.6 Limitations</A>
<b class="navlabel">Up:</b> <a class="sectref" href="profile.html" tppabs="http://www.python.org/doc/current/lib/profile.html">10. The Python Profiler</A>
<b class="navlabel">Next:</b> <a class="sectref" href="Profiler_Extensions.html" tppabs="http://www.python.org/doc/current/lib/Profiler_Extensions.html">10.8 Extensions  </A>
</DIV>
<!--End of Navigation Panel-->
<ADDRESS>
<hr>See <i><a href="about.html" tppabs="http://www.python.org/doc/current/lib/about.html">About this document...</a></i> for information on suggesting changes.
</ADDRESS>
</BODY>
</HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -