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

📄 ch32_02.htm

📁 编程珍珠,里面很多好用的代码,大家可以参考学习呵呵,
💻 HTM
字号:
<html><head><title>Benchmark (Programming Perl)</title><!-- STYLESHEET --><link rel="stylesheet" type="text/css" href="../style/style1.css"><!-- METADATA --><!--Dublin Core Metadata--><meta name="DC.Creator" content=""><meta name="DC.Date" content=""><meta name="DC.Format" content="text/xml" scheme="MIME"><meta name="DC.Generator" content="XSLT stylesheet, xt by James Clark"><meta name="DC.Identifier" content=""><meta name="DC.Language" content="en-US"><meta name="DC.Publisher" content="O'Reilly &amp; Associates, Inc."><meta name="DC.Source" content="" scheme="ISBN"><meta name="DC.Subject.Keyword" content=""><meta name="DC.Title" content="Benchmark"><meta name="DC.Type" content="Text.Monograph"></head><body><!-- START OF BODY --><!-- TOP BANNER --><img src="gifs/smbanner.gif" usemap="#banner-map" border="0" alt="Book Home"><map name="banner-map"><AREA SHAPE="RECT" COORDS="0,0,466,71" HREF="index.htm" ALT="Programming Perl"><AREA SHAPE="RECT" COORDS="467,0,514,18" HREF="jobjects/fsearch.htm" ALT="Search this book"></map><!-- TOP NAV BAR --><div class="navbar"><table width="515" border="0"><tr><td align="left" valign="top" width="172"><a href="ch32_01.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0"></a></td><td align="center" valign="top" width="171"><a href="ch32_01.htm">Chapter 32: Standard Modules</a></td><td align="right" valign="top" width="172"><a href="ch32_03.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0"></a></td></tr></table></div><hr width="515" align="left"><!-- SECTION BODY --><h2 class="sect1">32.2. Benchmark</h2><a name="INDEX-5800"></a><a name="INDEX-5801"></a><blockquote><pre class="programlisting">use Benchmark qw(timethese cmpthese timeit countit timestr);# You can always pass in code as strings:timethese $count, {    'Name1' =&gt; '...code1...',    'Name2' =&gt; '...code2...',};# Or as subroutines references:timethese $count, {    'Name1' =&gt; sub { ...code1... },    'Name2' =&gt; sub { ...code2... },};cmpthese $count, {    'Name1' =&gt; '...code1...',    'Name2' =&gt; '...code2...',};$t = timeit $count, '...code...';print "$count loops of code took:", timestr($t), "\n";$t = countit $time, '...code...';$count = $t-&gt;iters;print "$count loops of code took:", timestr($t), "\n";</pre></blockquote><p>The <tt class="literal">Benchmark</tt> module can help you determine whichof several possible choices executes the fastest.  The<tt class="literal">timethese</tt> function runs the specified code segmentsthe number of times requested and reports back how long each segmenttook.  You can get a nicely sorted comparison chart if you call<tt class="literal">cmpthese</tt> the same way.</p><p>Code segments may be given as function references instead of strings(in fact, they must be if you use lexical variables from the callingscope), but call overhead can influence the timings.  If you don't askfor enough iterations to get a good timing, the function emits awarning.</p><p>Lower-level interfaces are available that run just one piece of codeeither for some number of iterations (<tt class="literal">timeit</tt>) orfor some number of seconds (<tt class="literal">countit</tt>).  Thesefunctions return <tt class="literal">Benchmark</tt> objects (see the onlinedocumentation for a description).  With <tt class="literal">countit</tt>,you know it will run in enough time to avoid warnings, because youspecified a minimum run time.</p><p>To get the most out of the <tt class="literal">Benchmark</tt> module, you'llneed a good bit of practice.  It isn't usually enough to run a coupledifferent algorithms on the same data set, because the timings onlyreflect how well those algorithms did on that particular data set.  Toget a better feel for the general case, you'll need to run severalsets of benchmarks, varying the data sets used.</p><p>For example, suppose you wanted to know the best way to get a copyof a string without the last two characters.  You think of fourways to do so (there are, of course, several others): <tt class="literal">chop</tt>twice, copy and substitute, or use <tt class="literal">substr</tt> on either theleft- or righthand side of an assignment.  You test these algorithmson strings of length <tt class="literal">2</tt>, <tt class="literal">200</tt>, and <tt class="literal">20_000</tt>:<blockquote><pre class="programlisting">use Benchmark qw/countit cmpthese/;sub run($) { countit(5, @_) }for $size (2, 200, 20_000) {    $s = "." x $len;    print "\nDATASIZE = $size\n";    cmpthese {        chop2   =&gt; run q{            $t = $s; chop $t; chop $t;        },        subs    =&gt; run q{            ($t = $s) =~ s/..\Z//s;        },        lsubstr =&gt; run q{             $t = $s; substr($t, -2) = '';        },        rsubstr =&gt; run q{             $t = substr($s, 0, length($s)-2);        },    };}</pre></blockquote>which produces the following output:<blockquote><pre class="programlisting">DATASIZE = 2            Rate    subs lsubstr   chop2 rsubstrsubs    181399/s      --    -15%    -46%    -53%lsubstr 214655/s     18%      --    -37%    -44%chop2   338477/s     87%     58%      --    -12%rsubstr 384487/s    112%     79%     14%      --DATASIZE = 200            Rate    subs lsubstr rsubstr   chop2subs    200967/s      --    -18%    -24%    -34%lsubstr 246468/s     23%      --     -7%    -19%rsubstr 264428/s     32%      7%      --    -13%chop2   304818/s     52%     24%     15%      --DATASIZE = 20000          Rate rsubstr    subs lsubstr   chop2rsubstr 5271/s      --    -42%    -43%    -45%subs    9087/s     72%      --     -2%     -6%lsubstr 9260/s     76%      2%      --     -4%chop2   9660/s     83%      6%      4%      --</pre></blockquote>With small data sets, the "<tt class="literal">rsubstr</tt>" algorithm runs 14% faster thanthe "<tt class="literal">chop2</tt>" algorithm, but in large data sets, it runs 45% slower.On empty data sets (not shown here), the substitution mechanism is thefastest.  So there is often no best solution for all possible cases,and even these timings don't tell the whole story, since you're still atthe mercy of your operating system and the C library Perl was builtwith.  What's good for you may be bad for someone else.  It takes awhile to develop decent benchmarking skills.  In the meantime, it helpsto be a good liar.<a name="INDEX-5802"></a><a name="INDEX-5803"></a></p><!-- BOTTOM NAV BAR --><hr width="515" align="left"><div class="navbar"><table width="515" border="0"><tr><td align="left" valign="top" width="172"><a href="ch32_01.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0"></a></td><td align="center" valign="top" width="171"><a href="index.htm"><img src="../gifs/txthome.gif" alt="Home" border="0"></a></td><td align="right" valign="top" width="172"><a href="ch32_03.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0"></a></td></tr><tr><td align="left" valign="top" width="172">32.1. Listings by Type</td><td align="center" valign="top" width="171"><a href="index/index.htm"><img src="../gifs/index.gif" alt="Book Index" border="0"></a></td><td align="right" valign="top" width="172">32.3. Carp</td></tr></table></div><hr width="515" align="left"><!-- LIBRARY NAV BAR --><img src="../gifs/smnavbar.gif" usemap="#library-map" border="0" alt="Library Navigation Links"><p><font size="-1"><a href="copyrght.htm">Copyright &copy; 2001</a> O'Reilly &amp; Associates. All rights reserved.</font></p><map name="library-map"> <area shape="rect" coords="2,-1,79,99" href="../index.htm"><area shape="rect" coords="84,1,157,108" href="../perlnut/index.htm"><area shape="rect" coords="162,2,248,125" href="../prog/index.htm"><area shape="rect" coords="253,2,326,130" href="../advprog/index.htm"><area shape="rect" coords="332,1,407,112" href="../cookbook/index.htm"><area shape="rect" coords="414,2,523,103" href="../sysadmin/index.htm"></map><!-- END OF BODY --></body></html>

⌨️ 快捷键说明

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