benchmark.html
来自「perl教程」· HTML 代码 · 共 535 行 · 第 1/2 页
HTML
535 行
</dd>
<dd>
<p>FORMAT is the <a href="../lib/Pod/perlfunc.html#item_printf">printf(3)</a>-style format specifier (without the
leading '%') to use to print the times. It defaults to '5.2f'.</p>
</dd>
</li>
</dl>
<p>
</p>
<h2><a name="optional_exports">Optional Exports</a></h2>
<p>The following routines will be exported into your namespace
if you specifically ask that they be imported:</p>
<dl>
<dt><strong><a name="item_clearcache">clearcache ( COUNT )</a></strong>
<dd>
<p>Clear the cached time for COUNT rounds of the null loop.</p>
</dd>
</li>
<dt><strong><a name="item_clearallcache">clearallcache ( )</a></strong>
<dd>
<p>Clear all cached times.</p>
</dd>
</li>
<dt><strong><a name="item_cmpthese">cmpthese ( COUNT, CODEHASHREF, [ STYLE ] )</a></strong>
<dt><strong>cmpthese ( RESULTSHASHREF, [ STYLE ] )</strong>
<dd>
<p>Optionally calls timethese(), then outputs comparison chart. This:</p>
</dd>
<dd>
<pre>
<span class="variable">cmpthese</span><span class="operator">(</span> <span class="operator">-</span><span class="number">1</span><span class="operator">,</span> <span class="operator">{</span> <span class="string">a</span> <span class="operator">=></span> <span class="string">"++\$i"</span><span class="operator">,</span> <span class="string">b</span> <span class="operator">=></span> <span class="string">"\$i *= 2"</span> <span class="operator">}</span> <span class="operator">)</span> <span class="operator">;</span>
</pre>
</dd>
<dd>
<p>outputs a chart like:</p>
</dd>
<dd>
<pre>
Rate b a
b 2831802/s -- -61%
a 7208959/s 155% --</pre>
</dd>
<dd>
<p>This chart is sorted from slowest to fastest, and shows the percent speed
difference between each pair of tests.</p>
</dd>
<dd>
<p>c<cmpthese> can also be passed the data structure that <a href="#item_timethese"><code>timethese()</code></a> returns:</p>
</dd>
<dd>
<pre>
<span class="variable">$results</span> <span class="operator">=</span> <span class="variable">timethese</span><span class="operator">(</span> <span class="operator">-</span><span class="number">1</span><span class="operator">,</span> <span class="operator">{</span> <span class="string">a</span> <span class="operator">=></span> <span class="string">"++\$i"</span><span class="operator">,</span> <span class="string">b</span> <span class="operator">=></span> <span class="string">"\$i *= 2"</span> <span class="operator">}</span> <span class="operator">)</span> <span class="operator">;</span>
<span class="variable">cmpthese</span><span class="operator">(</span> <span class="variable">$results</span> <span class="operator">);</span>
</pre>
</dd>
<dd>
<p>in case you want to see both sets of results.</p>
</dd>
<dd>
<p>Returns a reference to an ARRAY of rows, each row is an ARRAY of cells from the
above chart, including labels. This:</p>
</dd>
<dd>
<pre>
<span class="keyword">my</span> <span class="variable">$rows</span> <span class="operator">=</span> <span class="variable">cmpthese</span><span class="operator">(</span> <span class="operator">-</span><span class="number">1</span><span class="operator">,</span> <span class="operator">{</span> <span class="string">a</span> <span class="operator">=></span> <span class="string">'++$i'</span><span class="operator">,</span> <span class="string">b</span> <span class="operator">=></span> <span class="string">'$i *= 2'</span> <span class="operator">}</span><span class="operator">,</span> <span class="string">"none"</span> <span class="operator">);</span>
</pre>
</dd>
<dd>
<p>returns a data structure like:</p>
</dd>
<dd>
<pre>
[
[ '', 'Rate', 'b', 'a' ],
[ 'b', '2885232/s', '--', '-59%' ],
[ 'a', '7099126/s', '146%', '--' ],
]</pre>
</dd>
<dd>
<p><strong>NOTE</strong>: This result value differs from previous versions, which returned
the <a href="#item_timethese"><code>timethese()</code></a> result structure. If you want that, just use the two
statement <a href="#item_timethese"><code>timethese</code></a>...<a href="#item_cmpthese"><code>cmpthese</code></a> idiom shown above.</p>
</dd>
<dd>
<p>Incidently, note the variance in the result values between the two examples;
this is typical of benchmarking. If this were a real benchmark, you would
probably want to run a lot more iterations.</p>
</dd>
</li>
<dt><strong><a name="item_countit">countit(TIME, CODE)</a></strong>
<dd>
<p>Arguments: TIME is the minimum length of time to run CODE for, and CODE is
the code to run. CODE may be either a code reference or a string to
be eval'd; either way it will be run in the caller's package.</p>
</dd>
<dd>
<p>TIME is <em>not</em> negative. <a href="#item_countit"><code>countit()</code></a> will run the loop many times to
calculate the speed of CODE before running it for TIME. The actual
time run for will usually be greater than TIME due to system clock
resolution, so it's best to look at the number of iterations divided
by the times that you are concerned with, not just the iterations.</p>
</dd>
<dd>
<p>Returns: a Benchmark object.</p>
</dd>
</li>
<dt><strong><a name="item_disablecache">disablecache ( )</a></strong>
<dd>
<p>Disable caching of timings for the null loop. This will force Benchmark
to recalculate these timings for each new piece of code timed.</p>
</dd>
</li>
<dt><strong><a name="item_enablecache">enablecache ( )</a></strong>
<dd>
<p>Enable caching of timings for the null loop. The time taken for COUNT
rounds of the null loop will be calculated only once for each
different COUNT used.</p>
</dd>
</li>
<dt><strong><a name="item_timesum">timesum ( T1, T2 )</a></strong>
<dd>
<p>Returns the sum of two Benchmark times as a Benchmark object suitable
for passing to timestr().</p>
</dd>
</li>
</dl>
<p>
</p>
<h2><a name="_hireswallclock">:hireswallclock</a></h2>
<p>If the Time::HiRes module has been installed, you can specify the
special tag <code>:hireswallclock</code> for Benchmark (if Time::HiRes is not
available, the tag will be silently ignored). This tag will cause the
wallclock time to be measured in microseconds, instead of integer
seconds. Note though that the speed computations are still conducted
in CPU time, not wallclock time.</p>
<p>
</p>
<hr />
<h1><a name="notes">NOTES</a></h1>
<p>The data is stored as a list of values from the time and times
functions:</p>
<pre>
($real, $user, $system, $children_user, $children_system, $iters)</pre>
<p>in seconds for the whole loop (not divided by the number of rounds).</p>
<p>The timing is done using <a href="../lib/Pod/perlfunc.html#item_time"><code>time(3)</code></a> and times(3).</p>
<p>Code is executed in the caller's package.</p>
<p>The time of the null loop (a loop with the same
number of rounds but empty loop body) is subtracted
from the time of the real loop.</p>
<p>The null loop times can be cached, the key being the
number of rounds. The caching can be controlled using
calls like these:</p>
<pre>
<span class="variable">clearcache</span><span class="operator">(</span><span class="variable">$key</span><span class="operator">);</span>
<span class="variable">clearallcache</span><span class="operator">();</span>
</pre>
<pre>
<span class="variable">disablecache</span><span class="operator">();</span>
<span class="variable">enablecache</span><span class="operator">();</span>
</pre>
<p>Caching is off by default, as it can (usually slightly) decrease
accuracy and does not usually noticably affect runtimes.</p>
<p>
</p>
<hr />
<h1><a name="examples">EXAMPLES</a></h1>
<p>For example,</p>
<pre>
<span class="keyword">use</span> <span class="variable">Benchmark</span> <span class="string">qw( cmpthese )</span> <span class="operator">;</span>
<span class="variable">$x</span> <span class="operator">=</span> <span class="number">3</span><span class="operator">;</span>
<span class="variable">cmpthese</span><span class="operator">(</span> <span class="operator">-</span><span class="number">5</span><span class="operator">,</span> <span class="operator">{</span>
<span class="string">a</span> <span class="operator">=></span> <span class="keyword">sub</span><span class="operator">{</span><span class="variable">$x</span><span class="operator">*</span><span class="variable">$x</span><span class="operator">},</span>
<span class="string">b</span> <span class="operator">=></span> <span class="keyword">sub</span><span class="operator">{</span><span class="variable">$x</span><span class="operator">**</span><span class="number">2</span><span class="operator">},</span>
<span class="operator">}</span> <span class="operator">);</span>
</pre>
<p>outputs something like this:</p>
<pre>
Benchmark: running a, b, each for at least 5 CPU seconds...
Rate b a
b 1559428/s -- -62%
a 4152037/s 166% --</pre>
<p>while</p>
<pre>
<span class="keyword">use</span> <span class="variable">Benchmark</span> <span class="string">qw( timethese cmpthese )</span> <span class="operator">;</span>
<span class="variable">$x</span> <span class="operator">=</span> <span class="number">3</span><span class="operator">;</span>
<span class="variable">$r</span> <span class="operator">=</span> <span class="variable">timethese</span><span class="operator">(</span> <span class="operator">-</span><span class="number">5</span><span class="operator">,</span> <span class="operator">{</span>
<span class="string">a</span> <span class="operator">=></span> <span class="keyword">sub</span><span class="operator">{</span><span class="variable">$x</span><span class="operator">*</span><span class="variable">$x</span><span class="operator">},</span>
<span class="string">b</span> <span class="operator">=></span> <span class="keyword">sub</span><span class="operator">{</span><span class="variable">$x</span><span class="operator">**</span><span class="number">2</span><span class="operator">},</span>
<span class="operator">}</span> <span class="operator">);</span>
<span class="variable">cmpthese</span> <span class="variable">$r</span><span class="operator">;</span>
</pre>
<p>outputs something like this:</p>
<pre>
Benchmark: running a, b, each for at least 5 CPU seconds...
a: 10 wallclock secs ( 5.14 usr + 0.13 sys = 5.27 CPU) @ 3835055.60/s (n=20210743)
b: 5 wallclock secs ( 5.41 usr + 0.00 sys = 5.41 CPU) @ 1574944.92/s (n=8520452)
Rate b a
b 1574945/s -- -59%
a 3835056/s 144% --</pre>
<p>
</p>
<hr />
<h1><a name="inheritance">INHERITANCE</a></h1>
<p>Benchmark inherits from no other class, except of course
for Exporter.</p>
<p>
</p>
<hr />
<h1><a name="caveats">CAVEATS</a></h1>
<p>Comparing eval'd strings with code references will give you
inaccurate results: a code reference will show a slightly slower
execution time than the equivalent eval'd string.</p>
<p>The real time timing is done using <a href="../lib/Pod/perlfunc.html#item_time"><code>time(2)</code></a> and
the granularity is therefore only one second.</p>
<p>Short tests may produce negative figures because perl
can appear to take longer to execute the empty loop
than a short test; try:</p>
<pre>
<span class="variable">timethis</span><span class="operator">(</span><span class="number">100</span><span class="operator">,</span><span class="string">'1'</span><span class="operator">);</span>
</pre>
<p>The system time of the null loop might be slightly
more than the system time of the loop with the actual
code and therefore the difference might end up being < 0.</p>
<p>
</p>
<hr />
<h1><a name="see_also">SEE ALSO</a></h1>
<p><a href="../lib/Devel/DProf.html">the Devel::DProf manpage</a> - a Perl code profiler</p>
<p>
</p>
<hr />
<h1><a name="authors">AUTHORS</a></h1>
<p>Jarkko Hietaniemi <<em><a href="mailto:jhi@iki.fi">jhi@iki.fi</a></em>>, Tim Bunce <<em><a href="mailto:Tim.Bunce@ig.co.uk">Tim.Bunce@ig.co.uk</a></em>></p>
<p>
</p>
<hr />
<h1><a name="modification_history">MODIFICATION HISTORY</a></h1>
<p>September 8th, 1994; by Tim Bunce.</p>
<p>March 28th, 1997; by Hugo van der Sanden: added support for code
references and the already documented 'debug' method; revamped
documentation.</p>
<p>April 04-07th, 1997: by Jarkko Hietaniemi, added the run-for-some-time
functionality.</p>
<p>September, 1999; by Barrie Slaymaker: math fixes and accuracy and
efficiency tweaks. Added cmpthese(). A result is now returned from
timethese(). Exposed <a href="#item_countit"><code>countit()</code></a> (was runfor()).</p>
<p>December, 2001; by Nicholas Clark: make <a href="#item_timestr"><code>timestr()</code></a> recognise the style 'none'
and return an empty string. If cmpthese is calling timethese, make it pass the
style in. (so that 'none' will suppress output). Make sub new dump its
debugging output to STDERR, to be consistent with everything else.
All bugs found while writing a regression test.</p>
<p>September, 2002; by Jarkko Hietaniemi: add ':hireswallclock' special tag.</p>
<p>February, 2004; by Chia-liang Kao: make cmpthese and timestr use time
statistics for children instead of parent when the style is 'nop'.</p>
</body>
</html>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?