📄 ch08_25.htm
字号:
<html><head><title>Benchmark (Perl in a Nutshell, 2nd Edition)</title><link rel="stylesheet" type="text/css" href="../style/style1.css" /><meta name="DC.Creator" content="Stephen Spainhour" /><meta name="DC.Format" content="text/xml" scheme="MIME" /><meta name="DC.Language" content="en-US" /><meta name="DC.Publisher" content="O'Reilly & Associates, Inc." /><meta name="DC.Source" scheme="ISBN" content="0596002416L" /><meta name="DC.Subject.Keyword" content="stuff" /><meta name="DC.Title" content="Perl in a Nutshell, 2nd Edition" /><meta name="DC.Type" content="Text.Monograph" /></head><body bgcolor="#ffffff"><img src="gifs/smbanner.gif" usemap="#banner-map" border="0" alt="Book Home" /><map name="banner-map"><area shape="rect" coords="1,-2,616,66" href="index.htm" alt="Java and XSLT" /><area shape="rect" coords="629,-11,726,25" href="jobjects/fsearch.htm" alt="Search this book" /></map><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="ch08_24.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0" /></a></td><td align="center" valign="top" width="228" /><td align="right" valign="top" width="228"><a href="ch08_26.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0" /></a></td></tr></table></div><h2 class="sect1">8.25. Benchmark</h2><p><a name="INDEX-1302" />Providesroutines for timing the execution of your code and formatting theresults. Inherits only from the Exporter class. It has the followingfunctions.</p><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>new</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>new <em class="replaceable">Benchmark</em></pre><p>Returns the current time. By getting the current time before andafter running the code, you can calculate the time it takes the codeto run.</p></div><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>clearallcache</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>clearallcache</pre><p>Clears the entire cache. Exported on request.</p></div><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>clearcache</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>clearcache (<em class="replaceable">count</em>)</pre><p>Clears the cached time for <em class="replaceable">count</em> rounds ofthe null loop. Exported on request.</p></div><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>debug</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>Benchmark->debug(<em class="replaceable">flag</em>)</pre><p>Enables or disables debugging by setting the<tt class="literal">$Benchmark::Debug</tt> flag.</p></div><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>disablecache</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>disablecache</pre><p>Disables use of the cache. Exported on request.</p></div><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>enablecache</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>enablecache</pre><p>Resumes caching. Exported on request.</p></div><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>timediff</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>timediff (<em class="replaceable">t1</em>, <em class="replaceable">t2</em>)</pre><p>Calculates the difference between two times and returns thedifference as a Benchmark object suitable for passing to<tt class="literal">timestr</tt>. Always exported.</p></div><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>timeit</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>timeit (<em class="replaceable">count</em>, <em class="replaceable">code</em>)</pre><p>Runs <em class="replaceable">code</em> and reports the time it took.Always exported. Takes the following arguments:</p><dl><dt><i><em class="replaceable"><tt>count</tt></em></i></dt><dd>The number of times to run the loop</p></dd><dt><i><em class="replaceable"><tt>code</tt></em></i></dt><dd>The code to run, specified as either a code reference or a string</p></dd></dl></div><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>timestr</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>timestr (<em class="replaceable">timediff</em>[, <em class="replaceable">style</em>[, <em class="replaceable">format</em>]])</pre><p>Converts times to printable strings. Always exported. Takes thefollowing arguments:</p><dl><dt><i><em class="replaceable"><tt>timediff</tt></em></i></dt><dd>The object containing the times to be formatted.</p></dd><dt><i><em class="replaceable"><tt>style</tt></em></i></dt><dd>The output format. The possible values of style are:</p><dl><dt><b><tt class="literal">all</tt></b></dt><dd>Shows all of the following times: wallclock, user, system, user timeof children, and system time of children</p></dd><dt><b><tt class="literal">auto</tt></b></dt><dd>Like <tt class="literal">all</tt>, except that if the children times areboth <tt class="literal">0</tt>, it acts like <tt class="literal">noc</tt></p></dd><dt><b><tt class="literal">noc</tt></b></dt><dd>Shows all except the two children times</p></dd><dt><b><tt class="literal">nop</tt></b></dt><dd>Shows only wallclock and the two children times</p></dd></dl></dd><dt><i><em class="replaceable"><tt>format</tt></em></i></dt><dd>Indicates the <tt class="literal">printf(3)</tt>-style format specifier(without the leading <tt class="literal">%</tt>) to use for printing thetimes. The default is <tt class="literal">5.2f</tt>.</p></dd></dl></div><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>timethese</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>timethese (<em class="replaceable">count</em>, \%<em class="replaceable">codehashref</em>[, <em class="replaceable">style</em>])</pre><p>Times each of several pieces of code and reports the resultsseparately. Always exported. Takes the following arguments:</p><dl><dt><i><em class="replaceable"><tt>count</tt></em></i></dt><dd>The number of times to run the loop.</p></dd><dt><i><em class="emphasis">\%</em><em class="replaceable"><tt>codehashref</tt></em></i></dt><dd>Reference to a hash in which the keys are names and the values areeither strings or code references; each key/value pair specifies apiece of code to run.</p></dd><dt><i><em class="replaceable"><tt>style</tt></em></i></dt><dd>Determines the format of the output. See <tt class="literal">timestr</tt>for the possible values of <em class="replaceable"><tt>style</tt></em>.</p></dd></dl></div><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>timethis</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>timethis (<em class="replaceable">count</em>, <em class="replaceable">code</em>[, <em class="replaceable">title</em>[, <em class="replaceable">style</em>]])</pre><p>Runs a chunk of code several times. Always exported. Takes thefollowing arguments:</p><dl><dt><i><em class="replaceable"><tt>count</tt></em></i></dt><dd>The number of times to run the loop.</p></dd><dt><i><em class="replaceable"><tt>code</tt></em></i></dt><dd>The code to run, specified as either a code reference or a string.</p></dd><dt><i><em class="replaceable"><tt>title</tt></em></i></dt><dd>The title of the result; default is <tt class="literal">"timethisCOUNT"</tt>.</p></dd><dt><i><em class="replaceable"><tt>style</tt></em></i></dt><dd>Determines the format of the output. See <tt class="literal">timestr</tt>for the possible values of <em class="replaceable"><tt>style</tt></em>.</p></dd></dl></div><hr width="684" align="left" /><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="ch08_24.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0" /></a></td><td align="center" valign="top" width="228"><a href="index.htm"><img src="../gifs/txthome.gif" alt="Home" border="0" /></a></td><td align="right" valign="top" width="228"><a href="ch08_26.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0" /></a></td></tr><tr><td align="left" valign="top" width="228">8.24. base</td><td align="center" valign="top" width="228"><a href="index/index.htm"><img src="../gifs/index.gif" alt="Book Index" border="0" /></a></td><td align="right" valign="top" width="228">8.26. bigint</td></tr></table></div><hr width="684" align="left" /><img src="../gifs/navbar.gif" usemap="#library-map" border="0" alt="Library Navigation Links" /><p><p><font size="-1"><a href="copyrght.htm">Copyright © 2002</a> O'Reilly & Associates. All rights reserved.</font></p><map name="library-map"><area shape="rect" coords="1,0,85,94" href="../index.htm"><area shape="rect" coords="86,1,178,103" href="../lwp/index.htm"><area shape="rect" coords="180,0,265,103" href="../lperl/index.htm"><area shape="rect" coords="267,0,353,105" href="../perlnut/index.htm"><area shape="rect" coords="354,1,446,115" href="../prog/index.htm"><area shape="rect" coords="448,0,526,132" href="../tk/index.htm"><area shape="rect" coords="528,1,615,119" href="../cookbook/index.htm"><area shape="rect" coords="617,0,690,135" href="../pxml/index.htm"></map></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -