perlperf.html 158 KB
Newer Older
1 2 3 4 5 6 7
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
  <title>perlperf - perldoc.perl.org</title>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  <meta http-equiv="Content-Language" content="en-gb">
8 9 10
  <link rel="search" type="application/opensearchdescription+xml" title="Search perldoc.perl.org" href="/static/search.xml"/>
  <link href="static/css-20100830.css" rel="stylesheet" rev="stylesheet" type="text/css" media="screen">
  <link href="static/exploreperl.css" rel="stylesheet" rev="stylesheet" type="text/css">
11 12 13 14 15 16 17 18 19
</head>

<body onLoad="perldoc.startup();" onPageShow="if (event.persisted) perldoc.startup();">
    <div id="page">
      
      <div id="header">
	<div id="homepage_link">
	  <a href="index.html"></a>
	</div>
20 21 22 23 24 25 26 27 28
	<div id="strapline">
	  Perl Programming Documentation
	</div>
	<div id="download_link" class="download">
	  <a href="http://www.perl.org/get.html">Download Perl</a>
	</div>
	<div id="explore_link" class="download">
	  <a id="explore_anchor" href="#">Explore</a>
	</div>
29 30 31 32 33
      </div>
      
      <div id="body">
        <div id="left_column">
          <div class="side_group">
34 35
            
	    <div class="side_panel doc_panel">
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
              <p>Manual</p>
              <ul>
                <li><a href="index-overview.html">Overview</a>
                <li><a href="index-tutorials.html">Tutorials</a>
                <li><a href="index-faq.html">FAQs</a>
                <li><a href="index-history.html">History / Changes</a>
                <li><a href="index-licence.html">License</a>
              </ul>
            </div>
            <div class="side_panel doc_panel">
              <p>Reference</p>
              <ul>
                <li><a href="index-language.html">Language</a>
                <li><a href="index-functions.html">Functions</a>
                <li><a href="perlop.html">Operators</a>
                <li><a href="perlvar.html">Special Variables</a>
                <li><a href="index-pragmas.html">Pragmas</a>
                <li><a href="index-utilities.html">Utilities</a>
54
                <li><a href="index-internals.html">Internals</a>
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
                <li><a href="index-platforms.html">Platform Specific</a>
              </ul>
            </div>
            <div class="side_panel doc_panel">
              <p>Modules</p>
              <ul>
		<li>
		
                
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		
                  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		
                  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		
                  
		
                  
		    
		  
		
                  
		
                  
		
		
                    <a href="index-modules-A.html">A</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-B.html">B</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-C.html">C</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-D.html">D</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-E.html">E</a>
                    
                      
                        <li>
                      
                    
                
                    <a href="index-modules-F.html">F</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-G.html">G</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-H.html">H</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-I.html">I</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-L.html">L</a>
                    
                      
                        <li>
                      
                    
                
                    <a href="index-modules-M.html">M</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-N.html">N</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-O.html">O</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-P.html">P</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-S.html">S</a>
                    
                      
                        <li>
                      
                    
                
                    <a href="index-modules-T.html">T</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-U.html">U</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-X.html">X</a>
                    
                
              </ul>
            </div>
277 278 279 280 281 282 283 284
            
	      <div class="side_panel doc_panel">
		<p>Tools</p>
		<ul>
		  <li><a href="preferences.html">Preferences</a>
		</ul>
	      </div>
            
285 286 287 288 289 290 291 292 293
          </div>
        </div>
        <div id="centre_column">
          <div id="content_header">
            <div id="title_bar">
              <div id="page_name">
                <h1>perlperf</h1>
              </div>
              <div id="perl_version">
294
                Perl 5 version 26.0 documentation
295
              </div>
296
              <div class="page_links" id="page_links_top">
297 298
                <a href="#" onClick="toolbar.goToTop();return false;">Go to top</a>
		
299 300
              </div>
	      <div class="page_links" id="page_links_bottom">
301
		
302
                  <a href="#" id="page_index_toggle">Show page index</a> &bull;
303
		
304 305 306 307 308 309 310
                <a href="#" id="recent_pages_toggle">Show recent pages</a>		
	      </div>
	      <div id="search_form">
		<form action="search.html" method="GET" id="search">
		  <input type="text" name="q" id="search_box" alt="Search">
		</form>
	      </div>
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
            </div>
            <div id="breadcrumbs">
                
    <a href="index.html">Home</a> &gt;
    
      
        <a href="index-language.html">Language reference</a> &gt;
      
    
    perlperf
  

            </div>
          </div>
          <div id="content_body">
	    <!--[if lt IE 7]>
 <div class="noscript">
   <p>
     <strong>It looks like you're using Internet Explorer 6. This is a very old
     browser which does not offer full support for modern websites.</strong>
   </p>
   <p>
     Unfortunately this means that this website will not work on
     your computer.
   </p>
   <p>
     Don't miss out though! To view the site (and get a better experience from
     many other websites), simply upgrade to
     <a href="http://www.microsoft.com/windows/Internet-explorer/default.aspx">Internet
Explorer 8</a>
     or download an alternative browser such as
     <a href="http://www.mozilla.com/en-US/firefox/firefox.html">Firefox</a>,
     <a href="http://www.apple.com/safari/download/">Safari</a>, or
     <a href="http://www.google.co.uk/chrome">Google Chrome</a>.
   </p>
   <p>
     All of these browsers are free. If you're using a PC at work, you may
     need to contact your IT administrator.
   </p>
 </div>
<![endif]-->
	    <noscript>
	      <div class="noscript">
	      <p>
                <strong>Please note: Many features of this site require JavaScript. You appear to have JavaScript disabled,
	        or are running a non-JavaScript capable web browser.</strong>
	      </p>
	      <p>
		To get the best experience, please enable JavaScript or download a modern web browser such as <a href="http://www.microsoft.com/windows/Internet-explorer/default.aspx">Internet Explorer 8</a>, <a href="http://www.mozilla.com/en-US/firefox/firefox.html">Firefox</a>, <a href="http://www.apple.com/safari/download/">Safari</a>, or <a href="http://www.google.co.uk/chrome">Google Chrome</a>.
              </p>
	      </div>
	    </noscript>
363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378

	    <div id="recent_pages" class="hud_container">
	      <div id="recent_pages_header" class="hud_header">
		<div id="recent_pages_close" class="hud_close"><a href="#" onClick="recentPages.hide();return false;"></a></div>
		<div id="recent_pages_title" class="hud_title"><span class="hud_span_top">Recently read</span></div>
		<div id="recent_pages_topright" class="hud_topright"></div>
	      </div>
	      <div id="recent_pages_content" class="hud_content">
	      </div>
	      <div id="recent_pages_footer" class="hud_footer">
		<div id="recent_pages_bottomleft" class="hud_bottomleft"></div>
		<div id="recent_pages_bottom" class="hud_bottom"><span class="hud_span_bottom"></span></div>
		<div id="recent_pages_resize" class="hud_resize"></div>
	      </div>
	    </div>
  
379 380 381 382 383 384 385 386 387 388 389 390 391 392
	    <div id="from_search"></div>
            <h1>perlperf</h1>


  <!--    -->
<ul><li><a href="#NAME">NAME</a><li><a href="#DESCRIPTION">DESCRIPTION</a><li><a href="#OVERVIEW">OVERVIEW</a><ul><li><a href="#ONE-STEP-SIDEWAYS">ONE STEP SIDEWAYS</a><li><a href="#ONE-STEP-FORWARD">ONE STEP FORWARD</a><li><a href="#ANOTHER-STEP-SIDEWAYS">ANOTHER STEP SIDEWAYS</a></ul><li><a href="#GENERAL-GUIDELINES">GENERAL GUIDELINES</a><li><a href="#BENCHMARKS">BENCHMARKS</a><ul><li><a href="#Assigning-and-Dereferencing-Variables.">Assigning and Dereferencing Variables.</a><li><a href="#Search-and-replace-or-tr">Search and replace or tr</a></ul><li><a href="#PROFILING-TOOLS">PROFILING TOOLS</a><ul><li><a href="#Devel%3a%3aDProf">Devel::DProf</a><li><a href="#Devel%3a%3aProfiler">Devel::Profiler</a><li><a href="#Devel%3a%3aSmallProf">Devel::SmallProf</a><li><a href="#Devel%3a%3aFastProf">Devel::FastProf</a><li><a href="#Devel%3a%3aNYTProf">Devel::NYTProf</a></ul><li><a href="#SORTING">SORTING</a><li><a href="#LOGGING">LOGGING</a><ul><li><a href="#Logging-if-DEBUG-(constant)">Logging if DEBUG (constant)</a></ul><li><a href="#POSTSCRIPT">POSTSCRIPT</a><li><a href="#SEE-ALSO">SEE ALSO</a><ul><li><a href="#PERLDOCS">PERLDOCS</a><li><a href="#MAN-PAGES">MAN PAGES</a><li><a href="#MODULES">MODULES</a><li><a href="#URLS">URLS</a></ul><li><a href="#AUTHOR">AUTHOR</a></ul><a name="NAME"></a><h1>NAME</h1>
<p>perlperf - Perl Performance and Optimization Techniques</p>
<a name="DESCRIPTION"></a><h1>DESCRIPTION</h1>
<p>This is an introduction to the use of performance and optimization techniques
which can be used with particular reference to perl programs.  While many perl
developers have come from other languages, and can use their prior knowledge
where appropriate, there are many other people who might benefit from a few
perl specific pointers.  If you want the condensed version, perhaps the best
advice comes from the renowned Japanese Samurai, Miyamoto Musashi, who said:</p>
393
<pre class="verbatim"><ol><li> <span class="q">&quot;Do Not Engage in Useless Activity&quot;</span></li></ol></pre><p>in 1645.</p>
394 395 396
<a name="OVERVIEW"></a><h1>OVERVIEW</h1>
<p>Perhaps the most common mistake programmers make is to attempt to optimize
their code before a program actually does anything useful - this is a bad idea.
397
There's no point in having an extremely fast program that doesn't work.  The
398 399 400 401 402 403 404
first job is to get a program to <i>correctly</i> do something <b>useful</b>, (not to
mention ensuring the test suite is fully functional), and only then to consider
optimizing it.  Having decided to optimize existing working code, there are
several simple but essential steps to consider which are intrinsic to any
optimization process.</p>
<a name="ONE-STEP-SIDEWAYS"></a><h2>ONE STEP SIDEWAYS</h2>
<p>Firstly, you need to establish a baseline time for the existing code, which
405
timing needs to be reliable and repeatable.  You'll probably want to use the
406
<code class="inline"><span class="w">Benchmark</span></code>
407
 or <code class="inline"><span class="w">Devel::NYTProf</span></code>
408
 modules, or something similar, for this step,
409
or perhaps the Unix system <code class="inline"><a class="l_k" href="functions/time.html">time</a></code> utility, whichever is appropriate.  See the
410 411 412 413 414 415 416
base of this document for a longer list of benchmarking and profiling modules,
and recommended further reading.</p>
<a name="ONE-STEP-FORWARD"></a><h2>ONE STEP FORWARD</h2>
<p>Next, having examined the program for <i>hot spots</i>, (places where the code
seems to run slowly), change the code with the intention of making it run
faster.  Using version control software, like <code class="inline"><span class="w">subversion</span></code>
, will ensure no
417 418
changes are irreversible.  It's too easy to fiddle here and fiddle there -
don't change too much at any one time or you might not discover which piece of
419 420
code <b>really</b> was the slow bit.</p>
<a name="ANOTHER-STEP-SIDEWAYS"></a><h2>ANOTHER STEP SIDEWAYS</h2>
421
<p>It's not enough to say: "that will make it run faster", you have to check it.
422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459
Rerun the code under control of the benchmarking or profiling modules, from the
first step above, and check that the new code executed the <b>same task</b> in
<i>less time</i>.  Save your work and repeat...</p>
<a name="GENERAL-GUIDELINES"></a><h1>GENERAL GUIDELINES</h1>
<p>The critical thing when considering performance is to remember there is no such
thing as a <code class="inline"><span class="w">Golden</span> <span class="w">Bullet</span></code>
, which is why there are no rules, only guidelines.</p>
<p>It is clear that inline code is going to be faster than subroutine or method
calls, because there is less overhead, but this approach has the disadvantage
of being less maintainable and comes at the cost of greater memory usage -
there is no such thing as a free lunch.  If you are searching for an element in
a list, it can be more efficient to store the data in a hash structure, and
then simply look to see whether the key is defined, rather than to loop through
the entire array using grep() for instance.  substr() may be (a lot) faster
than grep() but not as flexible, so you have another trade-off to access.  Your
code may contain a line which takes 0.01 of a second to execute which if you
call it 1,000 times, quite likely in a program parsing even medium sized files
for instance, you already have a 10 second delay, in just one single code
location, and if you call that line 100,000 times, your entire program will
slow down to an unbearable crawl.</p>
<p>Using a subroutine as part of your sort is a powerful way to get exactly what
you want, but will usually be slower than the built-in <i>alphabetic</i> <code class="inline">cmp</code>
 and
<i>numeric</i> <code class="inline">&lt;=&gt;</code>
 sort operators.  It is possible to make multiple
passes over your data, building indices to make the upcoming sort more
efficient, and to use what is known as the <code class="inline"><span class="w">OM</span></code>
 (Orcish Maneuver) to cache the
sort keys in advance.  The cache lookup, while a good idea, can itself be a
source of slowdown by enforcing a double pass over the data - once to setup the
cache, and once to sort the data.  Using <code class="inline"><a class="l_k" href="functions/pack.html">pack()</a></code> to extract the required sort
key into a consistent string can be an efficient way to build a single string
to compare, instead of using multiple sort keys, which makes it possible to use
the standard, written in <code class="inline"><span class="w">c</span></code>
 and fast, perl <code class="inline"><a class="l_k" href="functions/sort.html">sort()</a></code> function on the output,
and is the basis of the <code class="inline"><span class="w">GRT</span></code>
 (Guttman Rossler Transform).  Some string
combinations can slow the <code class="inline"><span class="w">GRT</span></code>
460
 down, by just being too plain complex for its
461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482
own good.</p>
<p>For applications using database backends, the standard <code class="inline"><span class="w">DBIx</span></code>
 namespace has
tries to help with keeping things nippy, not least because it tries to <i>not</i>
query the database until the latest possible moment, but always read the docs
which come with your choice of libraries.  Among the many issues facing
developers dealing with databases should remain aware of is to always use
<code class="inline"><span class="w">SQL</span></code>
 placeholders and to consider pre-fetching data sets when this might
prove advantageous.  Splitting up a large file by assigning multiple processes
to parsing a single file, using say <code class="inline"><span class="w">POE</span></code>
, <code class="inline"><span class="w">threads</span></code>
 or <code class="inline"><a class="l_k" href="functions/fork.html">fork</a></code> can also be a
useful way of optimizing your usage of the available <code class="inline"><span class="w">CPU</span></code>
 resources, though
this technique is fraught with concurrency issues and demands high attention to
detail.</p>
<p>Every case has a specific application and one or more exceptions, and there is
no replacement for running a few tests and finding out which method works best
for your particular environment, this is why writing optimal code is not an
exact science, and why we love using Perl so much - TMTOWTDI.</p>
<a name="BENCHMARKS"></a><h1>BENCHMARKS</h1>
483
<p>Here are a few examples to demonstrate usage of Perl's benchmarking tools.</p>
484
<a name="Assigning-and-Dereferencing-Variables."></a><h2>Assigning and Dereferencing Variables.</h2>
485
<p>I'm sure most of us have seen code which looks like, (or worse than), this:</p>
486
<pre class="verbatim"><ol><li> <a class="l_k" href="functions/if.html">if</a> <span class="s">(</span> <span class="i">$obj</span>-&gt;{<span class="w">_ref</span>}-&gt;{<span class="w">_myscore</span>} &gt;= <span class="i">$obj</span>-&gt;{<span class="w">_ref</span>}-&gt;{<span class="w">_yourscore</span>} <span class="s">)</span> <span class="s">{</span></li><li>     ...</li></ol></pre><p>This sort of code can be a real eyesore to read, as well as being very
487 488
sensitive to typos, and it's much clearer to dereference the variable
explicitly.  We're side-stepping the issue of working with object-oriented
489
programming techniques to encapsulate variable access via methods, only
490
accessible through an object.  Here we're just discussing the technical
491 492 493 494 495
implementation of choice, and whether this has an effect on performance.  We
can see whether this dereferencing operation, has any overhead by putting
comparative code in a file and running a <code class="inline"><span class="w">Benchmark</span></code>
 test.</p>
<p># dereference</p>
496
<pre class="verbatim"><ol><li> <span class="c">#!/usr/bin/perl</span></li><li></li><li> <a class="l_k" href="functions/use.html">use</a> <span class="w">strict</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/use.html">use</a> <span class="w">warnings</span><span class="sc">;</span></li><li></li><li> <a class="l_k" href="functions/use.html">use</a> <span class="w">Benchmark</span><span class="sc">;</span></li><li></li><li> <a class="l_k" href="functions/my.html">my</a> <span class="i">$ref</span> = <span class="s">{</span></li><li>         <span class="q">&#39;ref&#39;</span>   <span class="cm">=&gt;</span> <span class="s">{</span></li><li>             <span class="w">_myscore</span>    <span class="cm">=&gt;</span> <span class="q">&#39;100 + 1&#39;</span><span class="cm">,</span></li><li>             <span class="w">_yourscore</span>  <span class="cm">=&gt;</span> <span class="q">&#39;102 - 1&#39;</span><span class="cm">,</span></li><li>         <span class="s">}</span><span class="cm">,</span></li><li> <span class="s">}</span><span class="sc">;</span></li><li></li><li> <span class="i">timethese</span><span class="s">(</span><span class="n">1000000</span><span class="cm">,</span> <span class="s">{</span></li><li>         <span class="q">&#39;direct&#39;</span>       <span class="cm">=&gt;</span> <a class="l_k" href="functions/sub.html">sub</a> <span class="s">{</span></li><li>           <a class="l_k" href="functions/my.html">my</a> <span class="i">$x</span> = <span class="i">$ref</span>-&gt;{<span class="w">ref</span>}-&gt;{<span class="w">_myscore</span>} . <span class="i">$ref</span>-&gt;{<span class="w">ref</span>}-&gt;{<span class="w">_yourscore</span>} <span class="sc">;</span></li><li>         <span class="s">}</span><span class="cm">,</span></li><li>         <span class="q">&#39;dereference&#39;</span>  <span class="cm">=&gt;</span> <a class="l_k" href="functions/sub.html">sub</a> <span class="s">{</span></li><li>             <a class="l_k" href="functions/my.html">my</a> <span class="i">$ref</span>  = <span class="i">$ref</span>-&gt;{<span class="w">ref</span>}<span class="sc">;</span></li><li>             <a class="l_k" href="functions/my.html">my</a> <span class="i">$myscore</span> = <span class="i">$ref</span>-&gt;{<span class="w">_myscore</span>}<span class="sc">;</span></li><li>             <a class="l_k" href="functions/my.html">my</a> <span class="i">$yourscore</span> = <span class="i">$ref</span>-&gt;{<span class="w">_yourscore</span>}<span class="sc">;</span></li><li>             <a class="l_k" href="functions/my.html">my</a> <span class="i">$x</span> = <span class="i">$myscore</span> . <span class="i">$yourscore</span><span class="sc">;</span></li><li>         <span class="s">}</span><span class="cm">,</span></li><li> <span class="s">}</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>It's essential to run any timing measurements a sufficient number of times so
497 498 499 500 501 502 503
the numbers settle on a numerical average, otherwise each run will naturally
fluctuate due to variations in the environment, to reduce the effect of
contention for <code class="inline"><span class="w">CPU</span></code>
 resources and network bandwidth for instance.  Running
the above code for one million iterations, we can take a look at the report
output by the <code class="inline"><span class="w">Benchmark</span></code>
 module, to see which approach is the most effective.</p>
504
<pre class="verbatim"><ol><li> <span class="i">$&gt;</span> <span class="w">perl</span> <span class="w">dereference</span></li><li></li><li> <span class="w">Benchmark</span><span class="co">:</span> <span class="w">timing</span> <span class="n">1000000</span> <span class="w">iterations</span> <span class="w">of</span> <span class="w">dereference</span><span class="cm">,</span> <span class="w">direct</span>...</li><li> <span class="w">dereference</span><span class="co">:</span>  <span class="n">2</span> <span class="w">wallclock</span> <span class="w">secs</span> <span class="s">(</span> <span class="n">1.59</span> <span class="w">usr</span> +  <span class="n">0.00</span> <span class="w">sys</span> =  <span class="n">1.59</span> <span class="w">CPU</span><span class="s">)</span> <span class="i">@ 628930</span>.<span class="n">82</span>/<span class="q">s (n=1000000)</span></li><li>     <span class="q">     direct:  1 wallclock secs ( 1.20 usr +  0.00 sys =  1.20 CPU) @ 833333.33/s (n=1000000)</span></li></ol></pre><p>The difference is clear to see and the dereferencing approach is slower.  While
505 506 507
it managed to execute an average of 628,930 times a second during our test, the
direct approach managed to run an additional 204,403 times, unfortunately.
Unfortunately, because there are many examples of code written using the
508 509
multiple layer direct variable access, and it's usually horrible.  It is,
however, minusculy faster.  The question remains whether the minute gain is
510 511 512 513 514 515
actually worth the eyestrain, or the loss of maintainability.</p>
<a name="Search-and-replace-or-tr"></a><h2>Search and replace or tr</h2>
<p>If we have a string which needs to be modified, while a regex will almost
always be much more flexible, <code class="inline"><a class="l_k" href="functions/tr.html">tr</a></code>, an oft underused tool, can still be a
useful.  One scenario might be replace all vowels with another character.  The
regex solution might look like this:</p>
516 517
<pre class="verbatim"><ol><li> <span class="i">$str</span> =~ <span class="q">s/[aeiou]/x/g</span></li></ol></pre><p>The <code class="inline"><a class="l_k" href="functions/tr.html">tr</a></code> alternative might look like this:</p>
<pre class="verbatim"><ol><li> <span class="i">$str</span> =~ <span class="q">tr/aeiou/xxxxx/</span></li></ol></pre><p>We can put that into a test file which we can run to check which approach is
518 519 520 521
the fastest, using a global <code class="inline"><span class="i">$STR</span></code>
 variable to assign to the <code class="inline"><a class="l_k" href="functions/my.html">my</a> <span class="i">$str</span></code>

variable so as to avoid perl trying to optimize any of the work away by
522
noticing it's assigned only the once.</p>
523
<p># regex-transliterate</p>
524 525
<pre class="verbatim"><ol><li> <span class="c">#!/usr/bin/perl</span></li><li></li><li> <a class="l_k" href="functions/use.html">use</a> <span class="w">strict</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/use.html">use</a> <span class="w">warnings</span><span class="sc">;</span></li><li></li><li> <a class="l_k" href="functions/use.html">use</a> <span class="w">Benchmark</span><span class="sc">;</span></li><li></li><li> <a class="l_k" href="functions/my.html">my</a> <span class="i">$STR</span> = <span class="q">&quot;$$-this and that&quot;</span><span class="sc">;</span></li><li></li><li> <span class="i">timethese</span><span class="s">(</span> <span class="n">1000000</span><span class="cm">,</span> <span class="s">{</span></li><li> <span class="q">&#39;sr&#39;</span>  <span class="cm">=&gt;</span> <a class="l_k" href="functions/sub.html">sub</a> <span class="s">{</span> <a class="l_k" href="functions/my.html">my</a> <span class="i">$str</span> = <span class="i">$STR</span><span class="sc">;</span> <span class="i">$str</span> =~ <span class="q">s/[aeiou]/x/g</span><span class="sc">;</span> <a class="l_k" href="functions/return.html">return</a> <span class="i">$str</span><span class="sc">;</span> <span class="s">}</span><span class="cm">,</span></li><li> <span class="q">&#39;tr&#39;</span>  <span class="cm">=&gt;</span> <a class="l_k" href="functions/sub.html">sub</a> <span class="s">{</span> <a class="l_k" href="functions/my.html">my</a> <span class="i">$str</span> = <span class="i">$STR</span><span class="sc">;</span> <span class="i">$str</span> =~ <span class="q">tr/aeiou/xxxxx/</span><span class="sc">;</span> <a class="l_k" href="functions/return.html">return</a> <span class="i">$str</span><span class="sc">;</span> <span class="s">}</span><span class="cm">,</span></li><li> <span class="s">}</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>Running the code gives us our results:</p>
<pre class="verbatim"><ol><li> <span class="i">$&gt;</span> <span class="w">perl</span> <span class="w">regex</span>-<span class="w">transliterate</span></li><li></li><li> <span class="w">Benchmark</span><span class="co">:</span> <span class="w">timing</span> <span class="n">1000000</span> <span class="w">iterations</span> <span class="w">of</span> <span class="w">sr</span><span class="cm">,</span> <span class="q">tr...</span></li><li>         <span class="w">sr</span><span class="co">:</span>  <span class="n">2</span> <span class="w">wallclock</span> <span class="w">secs</span> <span class="s">(</span> <span class="n">1.19</span> <span class="w">usr</span> +  <span class="n">0.00</span> <span class="w">sys</span> =  <span class="n">1.19</span> <span class="w">CPU</span><span class="s">)</span> <span class="i">@ 840336</span>.<span class="n">13</span>/<span class="q">s (n=1000000)</span></li><li>         <span class="q">         tr:  0 wallclock secs ( 0.49 usr +  0.00 sys =  0.49 CPU) @ 2040816.33/s (n=1000000)</span></li></ol></pre><p>The <code class="inline"><a class="l_k" href="functions/tr.html">tr</a></code> version is a clear winner.  One solution is flexible, the other is
526
fast - and it's appropriately the programmer's choice which to use.</p>
527 528 529 530 531 532 533 534 535
<p>Check the <code class="inline"><span class="w">Benchmark</span></code>
 docs for further useful techniques.</p>
<a name="PROFILING-TOOLS"></a><h1>PROFILING TOOLS</h1>
<p>A slightly larger piece of code will provide something on which a profiler can
produce more extensive reporting statistics.  This example uses the simplistic
<code class="inline"><span class="w">wordmatch</span></code>
 program which parses a given input file and spews out a short
report on the contents.</p>
<p># wordmatch</p>
536
<pre class="verbatim"><ol><li> <span class="c">#!/usr/bin/perl</span></li><li></li><li> <a class="l_k" href="functions/use.html">use</a> <span class="w">strict</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/use.html">use</a> <span class="w">warnings</span><span class="sc">;</span></li><li></li><li> =<span class="w">head1</span> <span class="w">NAME</span></li><li></li><li> <span class="w">filewords</span> - <span class="w">word</span> <span class="w">analysis</span> <span class="w">of</span> <span class="w">input</span> <span class="w">file</span></li><li></li><li> =<span class="w">head1</span> <span class="w">SYNOPSIS</span></li><li></li><li>     <span class="w">filewords</span> -f <span class="w">inputfilename</span> <span class="s">[</span>-d<span class="s">]</span></li><li></li><li> =<span class="w">head1</span> <span class="w">DESCRIPTION</span></li><li></li><li> <span class="w">This</span> <span class="w">program</span> <span class="w">parses</span> <span class="w">the</span> <a class="l_k" href="functions/given.html">given</a> <span class="w">filename</span><span class="cm">,</span> <span class="w">specified</span> <span class="w">with</span> <span class="w">C</span><span class="q">&lt;-f&gt;</span><span class="cm">,</span> <a class="l_k" href="functions/and.html">and</a></li><li> <span class="w">displays</span> <span class="w">a</span> <span class="w">simple</span> <span class="w">analysis</span> <span class="w">of</span> <span class="w">the</span> <span class="w">words</span> <span class="w">found</span> <span class="w">therein</span>.  <span class="w">Use</span> <span class="w">the</span> <span class="w">C</span><span class="q">&lt;-d&gt;</span></li><li> switch <span class="w">to</span> <span class="w">enable</span> <span class="w">debugging</span> <span class="w">messages</span>.</li><li></li><li> =<span class="w">cut</span></li><li></li><li> <a class="l_k" href="functions/use.html">use</a> <span class="w">FileHandle</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/use.html">use</a> <span class="w">Getopt::Long</span><span class="sc">;</span></li><li></li><li> <a class="l_k" href="functions/my.html">my</a> <span class="i">$debug</span>   =  <span class="n">0</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/my.html">my</a> <span class="i">$file</span>    = <span class="q">&#39;&#39;</span><span class="sc">;</span></li><li></li><li> <a class="l_k" href="functions/my.html">my</a> <span class="i">$result</span> = <span class="w">GetOptions</span> <span class="s">(</span></li><li>     <span class="q">&#39;debug&#39;</span>         <span class="cm">=&gt;</span> \<span class="i">$debug</span><span class="cm">,</span></li><li>     <span class="q">&#39;file=s&#39;</span>        <span class="cm">=&gt;</span> \<span class="i">$file</span><span class="cm">,</span></li><li> <span class="s">)</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/die.html">die</a><span class="s">(</span><span class="q">&quot;invalid args&quot;</span><span class="s">)</span> <a class="l_k" href="functions/unless.html">unless</a> <span class="i">$result</span><span class="sc">;</span></li><li></li><li> <a class="l_k" href="functions/unless.html">unless</a> <span class="s">(</span> -f <span class="i">$file</span> <span class="s">)</span> <span class="s">{</span></li><li>     <a class="l_k" href="functions/die.html">die</a><span class="s">(</span><span class="q">&quot;Usage: $0 -f filename [-d]&quot;</span><span class="s">)</span><span class="sc">;</span></li><li> <span class="s">}</span></li><li> <a class="l_k" href="functions/my.html">my</a> <span class="i">$FH</span> = <span class="w">FileHandle</span><span class="w">-&gt;new</span><span class="s">(</span><span class="q">&quot;&lt; $file&quot;</span><span class="s">)</span></li><li>                               <a class="l_k" href="functions/or.html">or</a> <a class="l_k" href="functions/die.html">die</a><span class="s">(</span><span class="q">&quot;unable to open file($file): $!&quot;</span><span class="s">)</span><span class="sc">;</span></li><li></li><li> <a class="l_k" href="functions/my.html">my</a> <span class="i">$i_LINES</span> = <span class="n">0</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/my.html">my</a> <span class="i">$i_WORDS</span> = <span class="n">0</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/my.html">my</a> <span class="i">%count</span>   = <span class="s">(</span><span class="s">)</span><span class="sc">;</span></li><li></li><li> <a class="l_k" href="functions/my.html">my</a> <span class="i">@lines</span> = <span class="q">&lt;$FH&gt;</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/foreach.html">foreach</a> <a class="l_k" href="functions/my.html">my</a> <span class="i">$line</span> <span class="s">(</span> <span class="i">@lines</span> <span class="s">)</span> <span class="s">{</span></li><li>     <span class="i">$i_LINES</span>++<span class="sc">;</span></li><li>     <span class="i">$line</span> =~ <span class="q">s/\n//</span><span class="sc">;</span></li><li>     <a class="l_k" href="functions/my.html">my</a> <span class="i">@words</span> = <a class="l_k" href="functions/split.html">split</a><span class="s">(</span><span class="q">/ +/</span><span class="cm">,</span> <span class="i">$line</span><span class="s">)</span><span class="sc">;</span></li><li>     <a class="l_k" href="functions/my.html">my</a> <span class="i">$i_words</span> = <a class="l_k" href="functions/scalar.html">scalar</a><span class="s">(</span><span class="i">@words</span><span class="s">)</span><span class="sc">;</span></li><li>     <span class="i">$i_WORDS</span> = <span class="i">$i_WORDS</span> + <span class="i">$i_words</span><span class="sc">;</span></li><li>     <span class="i">debug</span><span class="s">(</span><span class="q">&quot;line: $i_LINES supplying $i_words words: @words&quot;</span><span class="s">)</span><span class="sc">;</span></li><li>     <a class="l_k" href="functions/my.html">my</a> <span class="i">$i_word</span> = <span class="n">0</span><span class="sc">;</span></li><li>     <a class="l_k" href="functions/foreach.html">foreach</a> <a class="l_k" href="functions/my.html">my</a> <span class="i">$word</span> <span class="s">(</span> <span class="i">@words</span> <span class="s">)</span> <span class="s">{</span></li><li>         <span class="i">$i_word</span>++<span class="sc">;</span></li><li>         <span class="i">$count</span>{<span class="i">$i_LINES</span>}{<span class="w">spec</span>} += <span class="i">matches</span><span class="s">(</span><span class="i">$i_word</span><span class="cm">,</span> <span class="i">$word</span><span class="cm">,</span></li><li>                                           <span class="q">&#39;[^a-zA-Z0-9]&#39;</span><span class="s">)</span><span class="sc">;</span></li><li>         <span class="i">$count</span>{<span class="i">$i_LINES</span>}{<span class="w">only</span>} += <span class="i">matches</span><span class="s">(</span><span class="i">$i_word</span><span class="cm">,</span> <span class="i">$word</span><span class="cm">,</span></li><li>                                           <span class="q">&#39;^[^a-zA-Z0-9]+$&#39;</span><span class="s">)</span><span class="sc">;</span></li><li>         <span class="i">$count</span>{<span class="i">$i_LINES</span>}{<span class="w">cons</span>} += <span class="i">matches</span><span class="s">(</span><span class="i">$i_word</span><span class="cm">,</span> <span class="i">$word</span><span class="cm">,</span></li><li>                                     <span class="q">&#39;^[(?i:bcdfghjklmnpqrstvwxyz)]+$&#39;</span><span class="s">)</span><span class="sc">;</span></li><li>         <span class="i">$count</span>{<span class="i">$i_LINES</span>}{<span class="w">vows</span>} += <span class="i">matches</span><span class="s">(</span><span class="i">$i_word</span><span class="cm">,</span> <span class="i">$word</span><span class="cm">,</span></li><li>                                           <span class="q">&#39;^[(?i:aeiou)]+$&#39;</span><span class="s">)</span><span class="sc">;</span></li><li>         <span class="i">$count</span>{<span class="i">$i_LINES</span>}{<span class="w">caps</span>} += <span class="i">matches</span><span class="s">(</span><span class="i">$i_word</span><span class="cm">,</span> <span class="i">$word</span><span class="cm">,</span></li><li>                                           <span class="q">&#39;^[(A-Z)]+$&#39;</span><span class="s">)</span><span class="sc">;</span></li><li>     <span class="s">}</span></li><li> <span class="s">}</span></li><li></li><li> <a class="l_k" href="functions/print.html">print</a> <span class="i">report</span><span class="s">(</span> <span class="i">%count</span> <span class="s">)</span><span class="sc">;</span></li><li></li><li><a name="matches"></a> sub <span class="m">matches</span> <span class="s">{</span></li><li>     <a class="l_k" href="functions/my.html">my</a> <span class="i">$i_wd</span>  = <a class="l_k" href="functions/shift.html">shift</a><span class="sc">;</span></li><li>     <a class="l_k" href="functions/my.html">my</a> <span class="i">$word</span>  = <a class="l_k" href="functions/shift.html">shift</a><span class="sc">;</span></li><li>     <a class="l_k" href="functions/my.html">my</a> <span class="i">$regex</span> = <a class="l_k" href="functions/shift.html">shift</a><span class="sc">;</span></li><li>     <a class="l_k" href="functions/my.html">my</a> <span class="i">$has</span> = <span class="n">0</span><span class="sc">;</span></li><li></li><li>     <a class="l_k" href="functions/if.html">if</a> <span class="s">(</span> <span class="i">$word</span> =~ <span class="q">/($regex)/</span> <span class="s">)</span> <span class="s">{</span></li><li>         <span class="i">$has</span>++ <a class="l_k" href="functions/if.html">if</a> <span class="i">$1</span><span class="sc">;</span></li><li>     <span class="s">}</span></li><li></li><li>     <span class="i">debug</span><span class="s">(</span> <span class="q">&quot;word: $i_wd &quot;</span></li><li>           . <span class="s">(</span><span class="i">$has</span> ? <span class="q">&#39;matches&#39;</span> <span class="co">:</span> <span class="q">&#39;does not match&#39;</span><span class="s">)</span></li><li>           . <span class="q">&quot; chars: /$regex/&quot;</span><span class="s">)</span><span class="sc">;</span></li><li></li><li>     <a class="l_k" href="functions/return.html">return</a> <span class="i">$has</span><span class="sc">;</span></li><li> <span class="s">}</span></li><li></li><li><a name="report"></a> sub <span class="m">report</span> <span class="s">{</span></li><li>     <a class="l_k" href="functions/my.html">my</a> <span class="i">%report</span> = <span class="i">@_</span><span class="sc">;</span></li><li>     <a class="l_k" href="functions/my.html">my</a> <span class="i">%rep</span><span class="sc">;</span></li><li></li><li>     <a class="l_k" href="functions/foreach.html">foreach</a> <a class="l_k" href="functions/my.html">my</a> <span class="i">$line</span> <span class="s">(</span> <a class="l_k" href="functions/keys.html">keys</a> <span class="i">%report</span> <span class="s">)</span> <span class="s">{</span></li><li>         <a class="l_k" href="functions/foreach.html">foreach</a> <a class="l_k" href="functions/my.html">my</a> <span class="i">$key</span> <span class="s">(</span> <a class="l_k" href="functions/keys.html">keys</a> <span class="i">%</span>{ <span class="i">$report</span>{<span class="i">$line</span>} } <span class="s">)</span> <span class="s">{</span></li><li>             <span class="i">$rep</span>{<span class="i">$key</span>} += <span class="i">$report</span>{<span class="i">$line</span>}{<span class="i">$key</span>}<span class="sc">;</span></li><li>         <span class="s">}</span></li><li>     <span class="s">}</span></li><li></li><li>     <a class="l_k" href="functions/my.html">my</a> <span class="i">$report</span> = <span class="q">qq|</span></li><li> <span class="q"> $0 report for $file:</span></li><li> <span class="q"> lines in file: $i_LINES</span></li><li> <span class="q"> words in file: $i_WORDS</span></li><li> <span class="q"> words with special (non-word) characters: $i_spec</span></li><li> <span class="q"> words with only special (non-word) characters: $i_only</span></li><li> <span class="q"> words with only consonants: $i_cons</span></li><li> <span class="q"> words with only capital letters: $i_caps</span></li><li> <span class="q"> words with only vowels: $i_vows</span></li><li> <span class="q"> |</span><span class="sc">;</span></li><li></li><li>     <a class="l_k" href="functions/return.html">return</a> <span class="i">$report</span><span class="sc">;</span></li><li> <span class="s">}</span></li><li></li><li><a name="debug"></a> sub <span class="m">debug</span> <span class="s">{</span></li><li>     <a class="l_k" href="functions/my.html">my</a> <span class="i">$message</span> = <a class="l_k" href="functions/shift.html">shift</a><span class="sc">;</span></li><li></li><li>     <a class="l_k" href="functions/if.html">if</a> <span class="s">(</span> <span class="i">$debug</span> <span class="s">)</span> <span class="s">{</span></li><li>         <a class="l_k" href="functions/print.html">print</a> <span class="i">STDERR</span> <span class="q">&quot;DBG: $message\n&quot;</span><span class="sc">;</span></li><li>     <span class="s">}</span></li><li> <span class="s">}</span></li><li></li><li> <a class="l_k" href="functions/exit.html">exit</a> <span class="n">0</span><span class="sc">;</span></li></ol></pre><a name="Devel%3a%3aDProf"></a><h2>Devel::DProf</h2>
537 538
<p>This venerable module has been the de-facto standard for Perl code profiling
for more than a decade, but has been replaced by a number of other modules
539
which have brought us back to the 21st century.  Although you're recommended to
540 541
evaluate your tool from the several mentioned here and from the CPAN list at
the base of this document, (and currently <a href="http://search.cpan.org/perldoc/Devel::NYTProf">Devel::NYTProf</a> seems to be the
542
weapon of choice - see below), we'll take a quick look at the output from
543
<a href="http://search.cpan.org/perldoc/Devel::DProf">Devel::DProf</a> first, to set a baseline for Perl profiling tools.  Run the
544 545 546 547
above program under the control of <code class="inline"><span class="w">Devel::DProf</span></code>
 by using the <code class="inline">-d</code>
 switch on
the command-line.</p>
548
<pre class="verbatim"><ol><li> <span class="i">$&gt;</span> <span class="w">perl</span> -d<span class="co">:</span><span class="w">DProf</span> <span class="w">wordmatch</span> -f <span class="w">perl5db</span>.<span class="w">pl</span></li><li></li><li> <span class="q">&lt;...multiple lines snipped...&gt;</span></li><li></li><li> <span class="w">wordmatch</span> <span class="w">report</span> <a class="l_k" href="functions/for.html">for</a> <span class="w">perl5db</span>.<span class="w">pl</span><span class="co">:</span></li><li> <span class="w">lines</span> <span class="w">in</span> <span class="w">file</span><span class="co">:</span> <span class="n">9428</span></li><li> <span class="w">words</span> <span class="w">in</span> <span class="w">file</span><span class="co">:</span> <span class="n">50243</span></li><li> <span class="w">words</span> <span class="w">with</span> <span class="w">special</span> <span class="s">(</span><span class="w">non</span>-<span class="w">word</span><span class="s">)</span> <span class="w">characters</span><span class="co">:</span> <span class="n">20480</span></li><li> <span class="w">words</span> <span class="w">with</span> <span class="w">only</span> <span class="w">special</span> <span class="s">(</span><span class="w">non</span>-<span class="w">word</span><span class="s">)</span> <span class="w">characters</span><span class="co">:</span> <span class="n">7790</span></li><li> <span class="w">words</span> <span class="w">with</span> <span class="w">only</span> <span class="w">consonants</span><span class="co">:</span> <span class="n">4801</span></li><li> <span class="w">words</span> <span class="w">with</span> <span class="w">only</span> <span class="w">capital</span> <span class="w">letters</span><span class="co">:</span> <span class="n">1316</span></li><li> <span class="w">words</span> <span class="w">with</span> <span class="w">only</span> <span class="w">vowels</span><span class="co">:</span> <span class="n">1701</span></li></ol></pre><p><code class="inline"><span class="w">Devel::DProf</span></code>
549 550 551 552 553 554 555 556 557
 produces a special file, called <i>tmon.out</i> by default, and
this file is read by the <code class="inline"><span class="w">dprofpp</span></code>
 program, which is already installed as part
of the <code class="inline"><span class="w">Devel::DProf</span></code>
 distribution.  If you call <code class="inline"><span class="w">dprofpp</span></code>
 with no options,
it will read the <i>tmon.out</i> file in the current directory and produce a human
readable statistics report of the run of your program.  Note that this may take
a little time.</p>
558
<pre class="verbatim"><ol><li> <span class="i">$&gt;</span> <span class="w">dprofpp</span></li><li></li><li> <span class="w">Total</span> <span class="w">Elapsed</span> <span class="w">Time</span> = <span class="n">2.951677</span> <span class="w">Seconds</span></li><li>   <span class="w">User</span>+<span class="w">System</span> <span class="w">Time</span> = <span class="n">2.871677</span> <span class="w">Seconds</span></li><li> <span class="w">Exclusive</span> <span class="w">Times</span></li><li> <span class="i">%Time</span> <span class="w">ExclSec</span> <span class="w">CumulS</span> <span class="c">#Calls sec/call Csec/c  Name</span></li><li>  <span class="n">102.</span>   <span class="n">2.945</span>  <span class="n">3.003</span> <span class="n">251215</span>   <span class="n">0.0000</span> <span class="n">0.0000</span>  <span class="w">main::matches</span></li><li>  <span class="n">2.40</span>   <span class="n">0.069</span>  <span class="n">0.069</span> <span class="n">260643</span>   <span class="n">0.0000</span> <span class="n">0.0000</span>  <span class="w">main::debug</span></li><li>  <span class="n">1.74</span>   <span class="n">0.050</span>  <span class="n">0.050</span>      <span class="n">1</span>   <span class="n">0.0500</span> <span class="n">0.0500</span>  <span class="w">main::report</span></li><li>  <span class="n">1.04</span>   <span class="n">0.030</span>  <span class="n">0.049</span>      <span class="n">4</span>   <span class="n">0.0075</span> <span class="n">0.0123</span>  <span class="w">main::BEGIN</span></li><li>  <span class="n">0.35</span>   <span class="n">0.010</span>  <span class="n">0.010</span>      <span class="n">3</span>   <span class="n">0.0033</span> <span class="n">0.0033</span>  <span class="w">Exporter::as_heavy</span></li><li>  <span class="n">0.35</span>   <span class="n">0.010</span>  <span class="n">0.010</span>      <span class="n">7</span>   <span class="n">0.0014</span> <span class="n">0.0014</span>  <span class="w">IO::File::BEGIN</span></li><li>  <span class="n">0.00</span>       - <span class="n">-0.000</span>      <span class="n">1</span>        -      -  <span class="w">Getopt::Long::FindOption</span></li><li>  <span class="n">0.00</span>       - <span class="n">-0.000</span>      <span class="n">1</span>        -      -  <span class="w">Symbol::BEGIN</span></li><li>  <span class="n">0.00</span>       - <span class="n">-0.000</span>      <span class="n">1</span>        -      -  <span class="w">Fcntl::BEGIN</span></li><li>  <span class="n">0.00</span>       - <span class="n">-0.000</span>      <span class="n">1</span>        -      -  <span class="w">Fcntl::bootstrap</span></li><li>  <span class="n">0.00</span>       - <span class="n">-0.000</span>      <span class="n">1</span>        -      -  <span class="w">warnings::BEGIN</span></li><li>  <span class="n">0.00</span>       - <span class="n">-0.000</span>      <span class="n">1</span>        -      -  <span class="w">IO::bootstrap</span></li><li>  <span class="n">0.00</span>       - <span class="n">-0.000</span>      <span class="n">1</span>        -      -  <span class="w">Getopt::Long::ConfigDefaults</span></li><li>  <span class="n">0.00</span>       - <span class="n">-0.000</span>      <span class="n">1</span>        -      -  <span class="w">Getopt::Long::Configure</span></li><li>  <span class="n">0.00</span>       - <span class="n">-0.000</span>      <span class="n">1</span>        -      -  <span class="w">Symbol::gensym</span></li></ol></pre><p><code class="inline"><span class="w">dprofpp</span></code>
559 560 561 562 563 564
 will produce some quite detailed reporting on the activity of the
<code class="inline"><span class="w">wordmatch</span></code>
 program.  The wallclock, user and system, times are at the top of
the analysis, and after this are the main columns defining which define the
report.  Check the <code class="inline"><span class="w">dprofpp</span></code>
 docs for details of the many options it supports.</p>
565
<p>See also <code class="inline"><a href="http://search.cpan.org/perldoc/Apache::DProf">Apache::DProf</a></code> which hooks <code class="inline"><span class="w">Devel::DProf</span></code>
566 567 568
 into <code class="inline"><span class="w">mod_perl</span></code>
.</p>
<a name="Devel%3a%3aProfiler"></a><h2>Devel::Profiler</h2>
569
<p>Let's take a look at the same program using a different profiler:
570 571 572 573 574 575 576
<code class="inline"><span class="w">Devel::Profiler</span></code>
, a drop-in Perl-only replacement for <code class="inline"><span class="w">Devel::DProf</span></code>
.  The
usage is very slightly different in that instead of using the special <code class="inline">-d:</code>
flag, you pull <code class="inline"><span class="w">Devel::Profiler</span></code>
 in directly as a module using <code class="inline">-M</code>
.</p>
577
<pre class="verbatim"><ol><li> <span class="i">$&gt;</span> <span class="w">perl</span> -<span class="w">MDevel::Profiler</span> <span class="w">wordmatch</span> -f <span class="w">perl5db</span>.<span class="w">pl</span></li><li></li><li> <span class="q">&lt;...multiple lines snipped...&gt;</span></li><li></li><li> <span class="w">wordmatch</span> <span class="w">report</span> <a class="l_k" href="functions/for.html">for</a> <span class="w">perl5db</span>.<span class="w">pl</span><span class="co">:</span></li><li> <span class="w">lines</span> <span class="w">in</span> <span class="w">file</span><span class="co">:</span> <span class="n">9428</span></li><li> <span class="w">words</span> <span class="w">in</span> <span class="w">file</span><span class="co">:</span> <span class="n">50243</span></li><li> <span class="w">words</span> <span class="w">with</span> <span class="w">special</span> <span class="s">(</span><span class="w">non</span>-<span class="w">word</span><span class="s">)</span> <span class="w">characters</span><span class="co">:</span> <span class="n">20480</span></li><li> <span class="w">words</span> <span class="w">with</span> <span class="w">only</span> <span class="w">special</span> <span class="s">(</span><span class="w">non</span>-<span class="w">word</span><span class="s">)</span> <span class="w">characters</span><span class="co">:</span> <span class="n">7790</span></li><li> <span class="w">words</span> <span class="w">with</span> <span class="w">only</span> <span class="w">consonants</span><span class="co">:</span> <span class="n">4801</span></li><li> <span class="w">words</span> <span class="w">with</span> <span class="w">only</span> <span class="w">capital</span> <span class="w">letters</span><span class="co">:</span> <span class="n">1316</span></li><li> <span class="w">words</span> <span class="w">with</span> <span class="w">only</span> <span class="w">vowels</span><span class="co">:</span> <span class="n">1701</span></li></ol></pre><p><code class="inline"><span class="w">Devel::Profiler</span></code>
578 579 580 581 582
 generates a tmon.out file which is compatible with the
<code class="inline"><span class="w">dprofpp</span></code>
 program, thus saving the construction of a dedicated statistics
reader program.  <code class="inline"><span class="w">dprofpp</span></code>
 usage is therefore identical to the above example.</p>
583
<pre class="verbatim"><ol><li> <span class="i">$&gt;</span> <span class="w">dprofpp</span></li><li></li><li> <span class="w">Total</span> <span class="w">Elapsed</span> <span class="w">Time</span> =   <span class="n">20.984</span> <span class="w">Seconds</span></li><li>   <span class="w">User</span>+<span class="w">System</span> <span class="w">Time</span> =   <span class="n">19.981</span> <span class="w">Seconds</span></li><li> <span class="w">Exclusive</span> <span class="w">Times</span></li><li> <span class="i">%Time</span> <span class="w">ExclSec</span> <span class="w">CumulS</span> <span class="c">#Calls sec/call Csec/c  Name</span></li><li>  <span class="n">49.0</span>   <span class="n">9.792</span> <span class="n">14.509</span> <span class="n">251215</span>   <span class="n">0.0000</span> <span class="n">0.0001</span>  <span class="w">main::matches</span></li><li>  <span class="n">24.4</span>   <span class="n">4.887</span>  <span class="n">4.887</span> <span class="n">260643</span>   <span class="n">0.0000</span> <span class="n">0.0000</span>  <span class="w">main::debug</span></li><li>  <span class="n">0.25</span>   <span class="n">0.049</span>  <span class="n">0.049</span>      <span class="n">1</span>   <span class="n">0.0490</span> <span class="n">0.0490</span>  <span class="w">main::report</span></li><li>  <span class="n">0.00</span>   <span class="n">0.000</span>  <span class="n">0.000</span>      <span class="n">1</span>   <span class="n">0.0000</span> <span class="n">0.0000</span>  <span class="w">Getopt::Long::GetOptions</span></li><li>  <span class="n">0.00</span>   <span class="n">0.000</span>  <span class="n">0.000</span>      <span class="n">2</span>   <span class="n">0.0000</span> <span class="n">0.0000</span>  <span class="w">Getopt::Long::ParseOptionSpec</span></li><li>  <span class="n">0.00</span>   <span class="n">0.000</span>  <span class="n">0.000</span>      <span class="n">1</span>   <span class="n">0.0000</span> <span class="n">0.0000</span>  <span class="w">Getopt::Long::FindOption</span></li><li>  <span class="n">0.00</span>   <span class="n">0.000</span>  <span class="n">0.000</span>      <span class="n">1</span>   <span class="n">0.0000</span> <span class="n">0.0000</span>  <span class="w">IO::File::new</span></li><li>  <span class="n">0.00</span>   <span class="n">0.000</span>  <span class="n">0.000</span>      <span class="n">1</span>   <span class="n">0.0000</span> <span class="n">0.0000</span>  <span class="w">IO::Handle::new</span></li><li>  <span class="n">0.00</span>   <span class="n">0.000</span>  <span class="n">0.000</span>      <span class="n">1</span>   <span class="n">0.0000</span> <span class="n">0.0000</span>  <span class="w">Symbol::gensym</span></li><li>  <span class="n">0.00</span>   <span class="n">0.000</span>  <span class="n">0.000</span>      <span class="n">1</span>   <span class="n">0.0000</span> <span class="n">0.0000</span>  <span class="w">IO::File::open</span></li></ol></pre><p>Interestingly we get slightly different results, which is mostly because the
584 585 586
algorithm which generates the report is different, even though the output file
format was allegedly identical.  The elapsed, user and system times are clearly
showing the time it took for <code class="inline"><span class="w">Devel::Profiler</span></code>
587
 to execute its own run, but
588 589 590 591 592
the column listings feel more accurate somehow than the ones we had earlier
from <code class="inline"><span class="w">Devel::DProf</span></code>
.  The 102% figure has disappeared, for example.  This is
where we have to use the tools at our disposal, and recognise their pros and
cons, before using them.  Interestingly, the numbers of calls for each
593
subroutine are identical in the two reports, it's the percentages which differ.
594 595
As the author of <code class="inline"><span class="w">Devel::Proviler</span></code>
 writes:</p>
596 597 598 599
<pre class="verbatim"><ol><li> ...<span class="w">running</span> <span class="w">HTML::Template&#39;s</span> <span class="w">test</span> <span class="w">suite</span> <span class="w">under</span> <span class="w">Devel::DProf</span> <span class="w">shows</span></li><li> <span class="i">output</span><span class="s">(</span><span class="s">)</span> <span class="w">taking</span> <span class="w">NO</span> <a class="l_k" href="functions/time.html">time</a> <span class="w">but</span> <span class="w">Devel::Profiler</span> <span class="w">shows</span> <span class="w">around</span> <span class="n">10</span>% <span class="w">of</span> <span class="w">the</span></li><li> <a class="l_k" href="functions/time.html">time</a> <span class="w">is</span> <span class="w">in</span> <span class="i">output</span><span class="s">(</span><span class="s">)</span>.  <span class="w">I</span> <span class="w">don&#39;t</span> <span class="w">know</span> <span class="w">which</span> <span class="w">to</span> <span class="w">trust</span> <span class="w">but</span> <a class="l_k" href="functions/my.html">my</a> <span class="w">gut</span> <span class="w">tells</span> <span class="w">me</span></li><li> <span class="w">something</span> <span class="w">is</span> <span class="w">wrong</span> <span class="w">with</span> <span class="w">Devel::DProf</span>.  <span class="i">HTML::Template::output</span><span class="s">(</span><span class="s">)</span> <span class="w">is</span> <span class="w">a</span></li><li> <span class="w">big</span> <span class="w">routine</span> <span class="w">that&#39;s</span> <span class="w">called</span> <a class="l_k" href="functions/for.html">for</a> <span class="w">every</span> <span class="w">test</span>. <span class="w">Either</span> <span class="w">way</span><span class="cm">,</span> <span class="w">something</span> <span class="w">needs</span></li><li> <span class="w">fixing</span>.</li></ol></pre><p>YMMV.</p>
<p>See also <code class="inline"><a href="http://search.cpan.org/perldoc/Devel::Apache::Profiler">Devel::Apache::Profiler</a></code> which hooks <code class="inline"><span class="w">Devel::Profiler</span></code>

into <code class="inline"><span class="w">mod_perl</span></code>
600 601 602 603 604 605 606 607
.</p>
<a name="Devel%3a%3aSmallProf"></a><h2>Devel::SmallProf</h2>
<p>The <code class="inline"><span class="w">Devel::SmallProf</span></code>
 profiler examines the runtime of your Perl program and
produces a line-by-line listing to show how many times each line was called,
and how long each line took to execute.  It is called by supplying the familiar
<code class="inline">-d</code>
 flag to Perl at runtime.</p>
608
<pre class="verbatim"><ol><li> <span class="i">$&gt;</span> <span class="w">perl</span> -d<span class="co">:</span><span class="w">SmallProf</span> <span class="w">wordmatch</span> -f <span class="w">perl5db</span>.<span class="w">pl</span></li><li></li><li> <span class="q">&lt;...multiple lines snipped...&gt;</span></li><li></li><li> <span class="w">wordmatch</span> <span class="w">report</span> <a class="l_k" href="functions/for.html">for</a> <span class="w">perl5db</span>.<span class="w">pl</span><span class="co">:</span></li><li> <span class="w">lines</span> <span class="w">in</span> <span class="w">file</span><span class="co">:</span> <span class="n">9428</span></li><li> <span class="w">words</span> <span class="w">in</span> <span class="w">file</span><span class="co">:</span> <span class="n">50243</span></li><li> <span class="w">words</span> <span class="w">with</span> <span class="w">special</span> <span class="s">(</span><span class="w">non</span>-<span class="w">word</span><span class="s">)</span> <span class="w">characters</span><span class="co">:</span> <span class="n">20480</span></li><li> <span class="w">words</span> <span class="w">with</span> <span class="w">only</span> <span class="w">special</span> <span class="s">(</span><span class="w">non</span>-<span class="w">word</span><span class="s">)</span> <span class="w">characters</span><span class="co">:</span> <span class="n">7790</span></li><li> <span class="w">words</span> <span class="w">with</span> <span class="w">only</span> <span class="w">consonants</span><span class="co">:</span> <span class="n">4801</span></li><li> <span class="w">words</span> <span class="w">with</span> <span class="w">only</span> <span class="w">capital</span> <span class="w">letters</span><span class="co">:</span> <span class="n">1316</span></li><li> <span class="w">words</span> <span class="w">with</span> <span class="w">only</span> <span class="w">vowels</span><span class="co">:</span> <span class="n">1701</span></li></ol></pre><p><code class="inline"><span class="w">Devel::SmallProf</span></code>
609
 writes it's output into a file called <i>smallprof.out</i>, by
610
default.  The format of the file looks like this:</p>
611
<pre class="verbatim"><ol><li> <span class="q">&lt;num&gt;</span> &lt;<a class="l_k" href="functions/time.html">time</a>&gt; <span class="q">&lt;ctime&gt;</span> &lt;<span class="w">line</span>&gt;<span class="co">:</span><span class="q">&lt;text&gt;</span></li></ol></pre><p>When the program has terminated, the output may be examined and sorted using
612 613
any standard text filtering utilities.  Something like the following may be
sufficient:</p>
614
<pre class="verbatim"><ol><li> <span class="i">$&gt;</span> <span class="w">cat</span> <span class="w">smallprof</span>.<span class="w">out</span> | <a class="l_k" href="functions/grep.html">grep</a> \<span class="w">d</span>*<span class="co">:</span> | <a class="l_k" href="functions/sort.html">sort</a> -<span class="w">k3</span> | <span class="w">tac</span> | <span class="w">head</span> -<span class="w">n20</span></li><li></li><li> <span class="n">251215</span>   <span class="n">1.65674</span>   <span class="n">7.68000</span>    <span class="n">75</span><span class="co">:</span> <a class="l_k" href="functions/if.html">if</a> <span class="s">(</span> <span class="i">$word</span> =~ <span class="q">/($regex)/</span> <span class="s">)</span> <span class="s">{</span></li><li> <span class="n">251215</span>   <span class="n">0.03264</span>   <span class="n">4.40000</span>    <span class="n">79</span><span class="co">:</span> <span class="i">debug</span><span class="s">(</span><span class="q">&quot;word: $i_wd &quot;</span>.<span class="s">(</span><span class="i">$has</span> ? <span class="q">&#39;matches&#39;</span> <span class="co">:</span></li><li> <span class="n">251215</span>   <span class="n">0.02693</span>   <span class="n">4.10000</span>    <span class="n">81</span><span class="co">:</span> <a class="l_k" href="functions/return.html">return</a> <span class="i">$has</span><span class="sc">;</span></li><li> <span class="n">260643</span>   <span class="n">0.02841</span>   <span class="n">4.07000</span>   <span class="n">128</span><span class="co">:</span> <a class="l_k" href="functions/if.html">if</a> <span class="s">(</span> <span class="i">$debug</span> <span class="s">)</span> <span class="s">{</span></li><li> <span class="n">260643</span>   <span class="n">0.02601</span>   <span class="n">4.04000</span>   <span class="n">126</span><span class="co">:</span> <a class="l_k" href="functions/my.html">my</a> <span class="i">$message</span> = <a class="l_k" href="functions/shift.html">shift</a><span class="sc">;</span></li><li> <span class="n">251215</span>   <span class="n">0.02641</span>   <span class="n">3.91000</span>    <span class="n">73</span><span class="co">:</span> <a class="l_k" href="functions/my.html">my</a> <span class="i">$has</span> = <span class="n">0</span><span class="sc">;</span></li><li> <span class="n">251215</span>   <span class="n">0.03311</span>   <span class="n">3.71000</span>    <span class="n">70</span><span class="co">:</span> <a class="l_k" href="functions/my.html">my</a> <span class="i">$i_wd</span>  = <a class="l_k" href="functions/shift.html">shift</a><span class="sc">;</span></li><li> <span class="n">251215</span>   <span class="n">0.02699</span>   <span class="n">3.69000</span>    <span class="n">72</span><span class="co">:</span> <a class="l_k" href="functions/my.html">my</a> <span class="i">$regex</span> = <a class="l_k" href="functions/shift.html">shift</a><span class="sc">;</span></li><li> <span class="n">251215</span>   <span class="n">0.02766</span>   <span class="n">3.68000</span>    <span class="n">71</span><span class="co">:</span> <a class="l_k" href="functions/my.html">my</a> <span class="i">$word</span>  = <a class="l_k" href="functions/shift.html">shift</a><span class="sc">;</span></li><li>  <span class="n">50243</span>   <span class="n">0.59726</span>   <span class="n">1.00000</span>    <span class="n">59</span><span class="co">:</span>  <span class="i">$count</span>{<span class="i">$i_LINES</span>}{<span class="w">cons</span>} =</li><li>  <span class="n">50243</span>   <span class="n">0.48175</span>   <span class="n">0.92000</span>    <span class="n">61</span><span class="co">:</span>  <span class="i">$count</span>{<span class="i">$i_LINES</span>}{<span class="w">spec</span>} =</li><li>  <span class="n">50243</span>   <span class="n">0.00644</span>   <span class="n">0.89000</span>    <span class="n">56</span><span class="co">:</span>  <a class="l_k" href="functions/my.html">my</a> <span class="i">$i_cons</span> = <span class="i">matches</span><span class="s">(</span><span class="i">$i_word</span><span class="cm">,</span> <span class="i">$word</span><span class="cm">,</span></li><li>  <span class="n">50243</span>   <span class="n">0.48837</span>   <span class="n">0.88000</span>    <span class="n">63</span><span class="co">:</span>  <span class="i">$count</span>{<span class="i">$i_LINES</span>}{<span class="w">caps</span>} =</li><li>  <span class="n">50243</span>   <span class="n">0.00516</span>   <span class="n">0.88000</span>    <span class="n">58</span><span class="co">:</span>  <span class="w">my</span> <span class="i">$i_caps</span> = <span class="i">matches</span><span class="s">(</span><span class="i">$i_word</span><span class="cm">,</span> <span class="i">$word</span><span class="cm">,</span> <span class="q">&#39;^[(A-</span></li><li>  <span class="q">  50243   0.00631   0.81000    54:  my $i_spec = matches($i_word, $word, &#39;</span><span class="s">[</span>^<span class="w">a</span>-</li><li>  <span class="n">50243</span>   <span class="n">0.00496</span>   <span class="n">0.80000</span>    <span class="n">57</span><span class="co">:</span>  <span class="w">my</span> <span class="i">$i_vows</span> = <span class="i">matches</span><span class="s">(</span><span class="i">$i_word</span><span class="cm">,</span> <span class="i">$word</span><span class="cm">,</span></li><li>  <span class="n">50243</span>   <span class="n">0.00688</span>   <span class="n">0.80000</span>    <span class="n">53</span><span class="co">:</span>  <span class="i">$i_word</span>++<span class="sc">;</span></li><li>  <span class="n">50243</span>   <span class="n">0.48469</span>   <span class="n">0.79000</span>    <span class="n">62</span><span class="co">:</span>  <span class="i">$count</span>{<span class="i">$i_LINES</span>}{<span class="w">only</span>} =</li><li>  <span class="n">50243</span>   <span class="n">0.48928</span>   <span class="n">0.77000</span>    <span class="n">60</span><span class="co">:</span>  <span class="i">$count</span>{<span class="i">$i_LINES</span>}{<span class="w">vows</span>} =</li><li>  <span class="n">50243</span>   <span class="n">0.00683</span>   <span class="n">0.75000</span>    <span class="n">55</span><span class="co">:</span>  <a class="l_k" href="functions/my.html">my</a> <span class="i">$i_only</span> = <span class="i">matches</span><span class="s">(</span><span class="i">$i_word</span><span class="cm">,</span> <span class="i">$word</span><span class="cm">,</span> <span class="q">&#39;^[^a-</span></li></ol></pre><p>You can immediately see a slightly different focus to the subroutine profiling
615 616 617 618
modules, and we start to see exactly which line of code is taking the most
time.  That regex line is looking a bit suspicious, for example.  Remember that
these tools are supposed to be used together, there is no single best way to
profile your code, you need to use the best tools for the job.</p>
619 620 621
<p>See also <code class="inline"><a href="http://search.cpan.org/perldoc/Apache::SmallProf">Apache::SmallProf</a></code> which hooks <code class="inline"><span class="w">Devel::SmallProf</span></code>
 into
<code class="inline"><span class="w">mod_perl</span></code>
622 623 624 625 626 627
.</p>
<a name="Devel%3a%3aFastProf"></a><h2>Devel::FastProf</h2>
<p><code class="inline"><span class="w">Devel::FastProf</span></code>
 is another Perl line profiler.  This was written with a view
to getting a faster line profiler, than is possible with for example
<code class="inline"><span class="w">Devel::SmallProf</span></code>
628
, because it's written in <code class="inline"><span class="w">C</span></code>
629 630 631 632
.  To use <code class="inline"><span class="w">Devel::FastProf</span></code>
,
supply the <code class="inline">-d</code>
 argument to Perl:</p>
633
<pre class="verbatim"><ol><li> <span class="i">$&gt;</span> <span class="w">perl</span> -d<span class="co">:</span><span class="w">FastProf</span> <span class="w">wordmatch</span> -f <span class="w">perl5db</span>.<span class="w">pl</span></li><li></li><li> <span class="q">&lt;...multiple lines snipped...&gt;</span></li><li></li><li> <span class="w">wordmatch</span> <span class="w">report</span> <a class="l_k" href="functions/for.html">for</a> <span class="w">perl5db</span>.<span class="w">pl</span><span class="co">:</span></li><li> <span class="w">lines</span> <span class="w">in</span> <span class="w">file</span><span class="co">:</span> <span class="n">9428</span></li><li> <span class="w">words</span> <span class="w">in</span> <span class="w">file</span><span class="co">:</span> <span class="n">50243</span></li><li> <span class="w">words</span> <span class="w">with</span> <span class="w">special</span> <span class="s">(</span><span class="w">non</span>-<span class="w">word</span><span class="s">)</span> <span class="w">characters</span><span class="co">:</span> <span class="n">20480</span></li><li> <span class="w">words</span> <span class="w">with</span> <span class="w">only</span> <span class="w">special</span> <span class="s">(</span><span class="w">non</span>-<span class="w">word</span><span class="s">)</span> <span class="w">characters</span><span class="co">:</span> <span class="n">7790</span></li><li> <span class="w">words</span> <span class="w">with</span> <span class="w">only</span> <span class="w">consonants</span><span class="co">:</span> <span class="n">4801</span></li><li> <span class="w">words</span> <span class="w">with</span> <span class="w">only</span> <span class="w">capital</span> <span class="w">letters</span><span class="co">:</span> <span class="n">1316</span></li><li> <span class="w">words</span> <span class="w">with</span> <span class="w">only</span> <span class="w">vowels</span><span class="co">:</span> <span class="n">1701</span></li></ol></pre><p><code class="inline"><span class="w">Devel::FastProf</span></code>
634 635 636 637
 writes statistics to the file <i>fastprof.out</i> in the current
directory.  The output file, which can be specified, can be interpreted by using
the <code class="inline"><span class="w">fprofpp</span></code>
 command-line program.</p>
638
<pre class="verbatim"><ol><li> <span class="i">$&gt;</span> <span class="w">fprofpp</span> | <span class="w">head</span> -<span class="w">n20</span></li><li></li><li> <span class="c"># fprofpp output format is:</span></li><li> <span class="c"># filename:line time count: source</span></li><li> <span class="w">wordmatch</span><span class="co">:</span><span class="n">75</span> <span class="n">3.93338</span> <span class="n">251215</span><span class="co">:</span> <a class="l_k" href="functions/if.html">if</a> <span class="s">(</span> <span class="i">$word</span> =~ <span class="q">/($regex)/</span> <span class="s">)</span> <span class="s">{</span></li><li> <span class="j">wordmatch:</span><span class="n">79</span> <span class="n">1.77774</span> <span class="n">251215</span><span class="co">:</span> <span class="i">debug</span><span class="s">(</span><span class="q">&quot;word: $i_wd &quot;</span>.<span class="s">(</span><span class="i">$has</span> ? <span class="q">&#39;matches&#39;</span> <span class="co">:</span> <span class="q">&#39;does not match&#39;</span><span class="s">)</span>.<span class="q">&quot; chars: /$regex/&quot;</span><span class="s">)</span><span class="sc">;</span></li><li> <span class="j">wordmatch:</span><span class="n">81</span> <span class="n">1.47604</span> <span class="n">251215</span><span class="co">:</span> <a class="l_k" href="functions/return.html">return</a> <span class="i">$has</span><span class="sc">;</span></li><li> <span class="j">wordmatch:</span><span class="n">126</span> <span class="n">1.43441</span> <span class="n">260643</span><span class="co">:</span> <a class="l_k" href="functions/my.html">my</a> <span class="i">$message</span> = <a class="l_k" href="functions/shift.html">shift</a><span class="sc">;</span></li><li> <span class="j">wordmatch:</span><span class="n">128</span> <span class="n">1.42156</span> <span class="n">260643</span><span class="co">:</span> <a class="l_k" href="functions/if.html">if</a> <span class="s">(</span> <span class="i">$debug</span> <span class="s">)</span> <span class="s">{</span></li><li> <span class="j">wordmatch:</span><span class="n">70</span> <span class="n">1.36824</span> <span class="n">251215</span><span class="co">:</span> <a class="l_k" href="functions/my.html">my</a> <span class="i">$i_wd</span>  = <a class="l_k" href="functions/shift.html">shift</a><span class="sc">;</span></li><li> <span class="j">wordmatch:</span><span class="n">71</span> <span class="n">1.36739</span> <span class="n">251215</span><span class="co">:</span> <a class="l_k" href="functions/my.html">my</a> <span class="i">$word</span>  = <a class="l_k" href="functions/shift.html">shift</a><span class="sc">;</span></li><li> <span class="j">wordmatch:</span><span class="n">72</span> <span class="n">1.35939</span> <span class="n">251215</span><span class="co">:</span> <a class="l_k" href="functions/my.html">my</a> <span class="i">$regex</span> = <a class="l_k" href="functions/shift.html">shift</a><span class="sc">;</span></li></ol></pre><p>Straightaway we can see that the number of times each line has been called is
639 640 641 642 643 644 645
identical to the <code class="inline"><span class="w">Devel::SmallProf</span></code>
 output, and the sequence is only very
slightly different based on the ordering of the amount of time each line took
to execute, <code class="inline">if ( $debug ) { </code> and <code class="inline"><a class="l_k" href="functions/my.html">my</a> <span class="i">$message</span> = <a class="l_k" href="functions/shift.html">shift</a><span class="sc">;</span></code>
, for example.  The
differences in the actual times recorded might be in the algorithm used
internally, or it could be due to system resource limitations or contention.</p>
646
<p>See also the <a href="http://search.cpan.org/perldoc/DBIx::Profile">DBIx::Profile</a> which will profile database queries running
647 648 649 650 651 652 653 654 655 656
under the <code class="inline"><span class="w">DBIx::</span>*</code>
 namespace.</p>
<a name="Devel%3a%3aNYTProf"></a><h2>Devel::NYTProf</h2>
<p><code class="inline"><span class="w">Devel::NYTProf</span></code>
 is the <b>next generation</b> of Perl code profiler, fixing many
shortcomings in other tools and implementing many cool features.  First of all it
can be used as either a <i>line</i> profiler, a <i>block</i> or a <i>subroutine</i>
profiler, all at once.  It can also use sub-microsecond (100ns) resolution on
systems which provide <code class="inline"><span class="i">clock_gettime</span><span class="s">(</span><span class="s">)</span></code>
.  It can be started and stopped even
657
by the program being profiled.  It's a one-line entry to profile <code class="inline"><span class="w">mod_perl</span></code>
658

659
applications.  It's written in <code class="inline"><span class="w">c</span></code>
660
 and is probably the fastest profiler
661
available for Perl.  The list of coolness just goes on.  Enough of that, let's
662 663 664
see how to it works - just use the familiar <code class="inline">-d</code>
 switch to plug it in and run
the code.</p>
665
<pre class="verbatim"><ol><li> <span class="i">$&gt;</span> <span class="w">perl</span> -d<span class="co">:</span><span class="w">NYTProf</span> <span class="w">wordmatch</span> -f <span class="w">perl5db</span>.<span class="w">pl</span></li><li></li><li> <span class="w">wordmatch</span> <span class="w">report</span> <a class="l_k" href="functions/for.html">for</a> <span class="w">perl5db</span>.<span class="w">pl</span><span class="co">:</span></li><li> <span class="w">lines</span> <span class="w">in</span> <span class="w">file</span><span class="co">:</span> <span class="n">9427</span></li><li> <span class="w">words</span> <span class="w">in</span> <span class="w">file</span><span class="co">:</span> <span class="n">50243</span></li><li> <span class="w">words</span> <span class="w">with</span> <span class="w">special</span> <span class="s">(</span><span class="w">non</span>-<span class="w">word</span><span class="s">)</span> <span class="w">characters</span><span class="co">:</span> <span class="n">20480</span></li><li> <span class="w">words</span> <span class="w">with</span> <span class="w">only</span> <span class="w">special</span> <span class="s">(</span><span class="w">non</span>-<span class="w">word</span><span class="s">)</span> <span class="w">characters</span><span class="co">:</span> <span class="n">7790</span></li><li> <span class="w">words</span> <span class="w">with</span> <span class="w">only</span> <span class="w">consonants</span><span class="co">:</span> <span class="n">4801</span></li><li> <span class="w">words</span> <span class="w">with</span> <span class="w">only</span> <span class="w">capital</span> <span class="w">letters</span><span class="co">:</span> <span class="n">1316</span></li><li> <span class="w">words</span> <span class="w">with</span> <span class="w">only</span> <span class="w">vowels</span><span class="co">:</span> <span class="n">1701</span></li></ol></pre><p><code class="inline"><span class="w">NYTProf</span></code>
666 667 668 669 670
 will generate a report database into the file <i>nytprof.out</i> by
default.  Human readable reports can be generated from here by using the
supplied <code class="inline"><span class="w">nytprofhtml</span></code>
 (HTML output) and <code class="inline"><span class="w">nytprofcsv</span></code>
 (CSV output) programs.
671
We've used the Unix system <code class="inline"><span class="w">html2text</span></code>
672 673
 utility to convert the
<i>nytprof/index.html</i> file for convenience here.</p>
674
<pre class="verbatim"><ol><li> <span class="i">$&gt;</span> <span class="w">html2text</span> <span class="w">nytprof</span>/<a class="l_k" href="functions/index.html">index</a>.<span class="w">html</span></li><li></li><li> <span class="w">Performance</span> <span class="w">Profile</span> <span class="w">Index</span></li><li> <span class="w">For</span> <span class="w">wordmatch</span></li><li>   <span class="w">Run</span> <span class="w">on</span> <span class="w">Fri</span> <span class="w">Sep</span> <span class="n">26</span> <span class="n">13</span><span class="co">:</span><span class="n">46</span><span class="co">:</span><span class="n">39</span> <span class="n">2008</span></li><li> <span class="w">Reported</span> <span class="w">on</span> <span class="w">Fri</span> <span class="w">Sep</span> <span class="n">26</span> <span class="n">13</span><span class="co">:</span><span class="n">47</span><span class="co">:</span><span class="n">23</span> <span class="n">2008</span></li><li></li><li>          <span class="w">Top</span> <span class="n">15</span> <span class="w">Subroutines</span> -- <span class="w">ordered</span> <span class="w">by</span> <span class="w">exclusive</span> <a class="l_k" href="functions/time.html">time</a></li><li> |<span class="w">Calls</span> |<span class="w">P</span> |<span class="w">F</span> |<span class="w">Inclusive</span>|<span class="w">Exclusive</span>|<span class="w">Subroutine</span>                          |</li><li> |      |  |  |<span class="w">Time</span>     |<span class="w">Time</span>     |                                    |</li><li> |<span class="n">251215</span>|<span class="n">5</span> |<span class="n">1</span> |<span class="n">13.09263</span> |<span class="n">10.47692</span> |<span class="w">main::</span>              |<span class="w">matches</span>        |</li><li> |<span class="n">260642</span>|<span class="n">2</span> |<span class="n">1</span> |<span class="n">2.71199</span>  |<span class="n">2.71199</span>  |<span class="w">main::</span>              |<span class="w">debug</span>          |</li><li> |<span class="n">1</span>     |<span class="n">1</span> |<span class="n">1</span> |<span class="n">0.21404</span>  |<span class="n">0.21404</span>  |<span class="w">main::</span>              |<span class="w">report</span>         |</li><li> |<span class="n">2</span>     |<span class="n">2</span> |<span class="n">2</span> |<span class="n">0.00511</span>  |<span class="n">0.00511</span>  |<span class="w">XSLoader::</span>          |<span class="w">load</span> <span class="s">(</span><span class="w">xsub</span><span class="s">)</span>    |</li><li> |<span class="n">14</span>    |<span class="n">14</span>|<span class="n">7</span> |<span class="n">0.00304</span>  |<span class="n">0.00298</span>  |<span class="w">Exporter::</span>          |<span class="w">import</span>         |</li><li> |<span class="n">3</span>     |<span class="n">1</span> |<span class="n">1</span> |<span class="n">0.00265</span>  |<span class="n">0.00254</span>  |<span class="w">Exporter::</span>          |<span class="w">as_heavy</span>       |</li><li> |<span class="n">10</span>    |<span class="n">10</span>|<span class="n">4</span> |<span class="n">0.00140</span>  |<span class="n">0.00140</span>  |<span class="w">vars::</span>              |<span class="w">import</span>         |</li><li> |<span class="n">13</span>    |<span class="n">13</span>|<span class="n">1</span> |<span class="n">0.00129</span>  |<span class="n">0.00109</span>  |<span class="w">constant::</span>          |<span class="w">import</span>         |</li><li> |<span class="n">1</span>     |<span class="n">1</span> |<span class="n">1</span> |<span class="n">0.00360</span>  |<span class="n">0.00096</span>  |<span class="w">FileHandle::</span>        |<span class="w">import</span>         |</li><li> |<span class="n">3</span>     |<span class="n">3</span> |<span class="n">3</span> |<span class="n">0.00086</span>  |<span class="n">0.00074</span>  |<span class="w">warnings::register::</span>|<span class="w">import</span>         |</li><li> |<span class="n">9</span>     |<span class="n">3</span> |<span class="n">1</span> |<span class="n">0.00036</span>  |<span class="n">0.00036</span>  |<span class="w">strict::</span>            |<span class="w">bits</span>           |</li><li> |<span class="n">13</span>    |<span class="n">13</span>|<span class="n">13</span>|<span class="n">0.00032</span>  |<span class="n">0.00029</span>  |<span class="w">strict::</span>            |<span class="w">import</span>         |</li><li> |<span class="n">2</span>     |<span class="n">2</span> |<span class="n">2</span> |<span class="n">0.00020</span>  |<span class="n">0.00020</span>  |<span class="w">warnings::</span>          |<span class="w">import</span>         |</li><li> |<span class="n">2</span>     |<span class="n">1</span> |<span class="n">1</span> |<span class="n">0.00020</span>  |<span class="n">0.00020</span>  |<span class="w">Getopt::Long::</span>      |<span class="w">ParseOptionSpec</span>|</li><li> |<span class="n">7</span>     |<span class="n">7</span> |<span class="n">6</span> |<span class="n">0.00043</span>  |<span class="n">0.00020</span>  |<span class="w">strict::</span>            |<span class="w">unimport</span>       |</li><li></li><li> <span class="w">For</span> <span class="w">more</span> <span class="w">information</span> <span class="w">see</span> <span class="w">the</span> <span class="w">full</span> <span class="w">list</span> <span class="w">of</span> <span class="n">189</span> <span class="w">subroutines</span>.</li></ol></pre><p>The first part of the report already shows the critical information regarding
675 676
which subroutines are using the most time.  The next gives some statistics
about the source files profiled.</p>
677
<pre class="verbatim"><ol><li>         <span class="w">Source</span> <span class="w">Code</span> <span class="w">Files</span> -- <span class="w">ordered</span> <span class="w">by</span> <span class="w">exclusive</span> <a class="l_k" href="functions/time.html">time</a> <span class="w">then</span> <span class="w">name</span></li><li> |<span class="w">Stmts</span>  |<span class="w">Exclusive</span>|<span class="w">Avg</span>.   |<span class="w">Reports</span>                     |<span class="w">Source</span> <span class="w">File</span>         |</li><li> |       |<span class="w">Time</span>     |       |                            |                    |</li><li> |<span class="n">2699761</span>|<span class="n">15.66654</span> |<span class="n">6e-06</span>  |<span class="w">line</span>   .    <span class="w">block</span>   .    <a class="l_k" href="functions/sub.html">sub</a>|<span class="w">wordmatch</span>           |</li><li> |<span class="n">35</span>     |<span class="n">0.02187</span>  |<span class="n">0.00062</span>|<span class="w">line</span>   .    <span class="w">block</span>   .    <a class="l_k" href="functions/sub.html">sub</a>|<span class="w">IO</span>/<span class="w">Handle</span>.<span class="w">pm</span>        |</li><li> |<span class="n">274</span>    |<span class="n">0.01525</span>  |<span class="n">0.00006</span>|<span class="w">line</span>   .    <span class="w">block</span>   .    <a class="l_k" href="functions/sub.html">sub</a>|<span class="w">Getopt</span>/<span class="w">Long</span>.<span class="w">pm</span>      |</li><li> |<span class="n">20</span>     |<span class="n">0.00585</span>  |<span class="n">0.00029</span>|<span class="w">line</span>   .    <span class="w">block</span>   .    <a class="l_k" href="functions/sub.html">sub</a>|<span class="w">Fcntl</span>.<span class="w">pm</span>            |</li><li> |<span class="n">128</span>    |<span class="n">0.00340</span>  |<span class="n">0.00003</span>|<span class="w">line</span>   .    <span class="w">block</span>   .    <a class="l_k" href="functions/sub.html">sub</a>|<span class="w">Exporter</span>/<span class="w">Heavy</span>.<span class="w">pm</span>   |</li><li> |<span class="n">42</span>     |<span class="n">0.00332</span>  |<span class="n">0.00008</span>|<span class="w">line</span>   .    <span class="w">block</span>   .    <a class="l_k" href="functions/sub.html">sub</a>|<span class="w">IO</span>/<span class="w">File</span>.<span class="w">pm</span>          |</li><li> |<span class="n">261</span>    |<span class="n">0.00308</span>  |<span class="n">0.00001</span>|<span class="w">line</span>   .    <span class="w">block</span>   .    <a class="l_k" href="functions/sub.html">sub</a>|<span class="w">Exporter</span>.<span class="w">pm</span>         |</li><li> |<span class="n">323</span>    |<span class="n">0.00248</span>  |<span class="n">8e-06</span>  |<span class="w">line</span>   .    <span class="w">block</span>   .    <a class="l_k" href="functions/sub.html">sub</a>|<span class="w">constant</span>.<span class="w">pm</span>         |</li><li> |<span class="n">12</span>     |<span class="n">0.00246</span>  |<span class="n">0.00021</span>|<span class="w">line</span>   .    <span class="w">block</span>   .    <a class="l_k" href="functions/sub.html">sub</a>|<span class="w">File</span>/<span class="w">Spec</span>/<span class="w">Unix</span>.<span class="w">pm</span>   |</li><li> |<span class="n">191</span>    |<span class="n">0.00240</span>  |<span class="n">0.00001</span>|<span class="w">line</span>   .    <span class="w">block</span>   .    <a class="l_k" href="functions/sub.html">sub</a>|<span class="w">vars</span>.<span class="w">pm</span>             |</li><li> |<span class="n">77</span>     |<span class="n">0.00201</span>  |<span class="n">0.00003</span>|<span class="w">line</span>   .    <span class="w">block</span>   .    <a class="l_k" href="functions/sub.html">sub</a>|<span class="w">FileHandle</span>.<span class="w">pm</span>       |</li><li> |<span class="n">12</span>     |<span class="n">0.00198</span>  |<span class="n">0.00016</span>|<span class="w">line</span>   .    <span class="w">block</span>   .    <a class="l_k" href="functions/sub.html">sub</a>|<span class="w">Carp</span>.<span class="w">pm</span>             |</li><li> |<span class="n">14</span>     |<span class="n">0.00175</span>  |<span class="n">0.00013</span>|<span class="w">line</span>   .    <span class="w">block</span>   .    <a class="l_k" href="functions/sub.html">sub</a>|<span class="w">Symbol</span>.<span class="w">pm</span>           |</li><li> |<span class="n">15</span>     |<span class="n">0.00130</span>  |<span class="n">0.00009</span>|<span class="w">line</span>   .    <span class="w">block</span>   .    <a class="l_k" href="functions/sub.html">sub</a>|<span class="w">IO</span>.<span class="w">pm</span>               |</li><li> |<span class="n">22</span>     |<span class="n">0.00120</span>  |<span class="n">0.00005</span>|<span class="w">line</span>   .    <span class="w">block</span>   .    <a class="l_k" href="functions/sub.html">sub</a>|<span class="w">IO</span>/<span class="w">Seekable</span>.<span class="w">pm</span>      |</li><li> |<span class="n">198</span>    |<span class="n">0.00085</span>  |<span class="n">4e-06</span>  |<span class="w">line</span>   .    <span class="w">block</span>   .    <a class="l_k" href="functions/sub.html">sub</a>|<span class="w">warnings</span>/<span class="w">register</span>.<span class="w">pm</span>|</li><li> |<span class="n">114</span>    |<span class="n">0.00080</span>  |<span class="n">7e-06</span>  |<span class="w">line</span>   .    <span class="w">block</span>   .    <a class="l_k" href="functions/sub.html">sub</a>|<span class="w">strict</span>.<span class="w">pm</span>           |</li><li> |<span class="n">47</span>     |<span class="n">0.00068</span>  |<span class="n">0.00001</span>|<span class="w">line</span>   .    <span class="w">block</span>   .    <a class="l_k" href="functions/sub.html">sub</a>|<span class="w">warnings</span>.<span class="w">pm</span>         |</li><li> |<span class="n">27</span>     |<span class="n">0.00054</span>  |<span class="n">0.00002</span>|<span class="w">line</span>   .    <span class="w">block</span>   .    <a class="l_k" href="functions/sub.html">sub</a>|<span class="w">overload</span>.<span class="w">pm</span>         |</li><li> |<span class="n">9</span>      |<span class="n">0.00047</span>  |<span class="n">0.00005</span>|<span class="w">line</span>   .    <span class="w">block</span>   .    <a class="l_k" href="functions/sub.html">sub</a>|<span class="w">SelectSaver</span>.<span class="w">pm</span>      |</li><li> |<span class="n">13</span>     |<span class="n">0.00045</span>  |<span class="n">0.00003</span>|<span class="w">line</span>   .    <span class="w">block</span>   .    <a class="l_k" href="functions/sub.html">sub</a>|<span class="w">File</span>/<span class="w">Spec</span>.<span class="w">pm</span>        |</li><li> |<span class="n">2701595</span>|<span class="n">15.73869</span> |       |<span class="w">Total</span>                       |</li><li> |<span class="n">128647</span> |<span class="n">0.74946</span>  |       |<span class="w">Average</span>                     |</li><li> |       |<span class="n">0.00201</span>  |<span class="n">0.00003</span>|<span class="w">Median</span>                      |</li><li> |       |<span class="n">0.00121</span>  |<span class="n">0.00003</span>|<span class="w">Deviation</span>                   |</li><li></li><li> <span class="w">Report</span> <span class="w">produced</span> <span class="w">by</span> <span class="w">the</span> <span class="w">NYTProf</span> <span class="n">2.03</span> <span class="w">Perl</span> <span class="w">profiler</span><span class="cm">,</span> <span class="w">developed</span> <span class="w">by</span> <span class="w">Tim</span> <span class="w">Bunce</span> <a class="l_k" href="functions/and.html">and</a></li><li> <span class="w">Adam</span> <span class="w">Kaplan</span>.</li></ol></pre><p>At this point, if you're using the <i>html</i> report, you can click through the
678
various links to bore down into each subroutine and each line of code.  Because
679 680
we're using the text reporting here, and there's a whole directory full of
reports built for each source file, we'll just display a part of the
681 682
corresponding <i>wordmatch-line.html</i> file, sufficient to give an idea of the
sort of output you can expect from this cool tool.</p>
683 684 685 686
<pre class="verbatim"><ol><li> <span class="i">$&gt;</span> <span class="w">html2text</span> <span class="w">nytprof</span>/<span class="w">wordmatch</span>-<span class="w">line</span>.<span class="w">html</span></li><li></li><li><a name="view"></a> <span class="w">Performance</span> <span class="w">Profile</span> -- -<span class="w">block</span> <span class="w">view</span>-.-<span class="w">line</span> <span class="w">view</span>-.-sub <span class="m">view</span>-</li><li> <span class="w">For</span> <span class="w">wordmatch</span></li><li> <span class="w">Run</span> <span class="w">on</span> <span class="w">Fri</span> <span class="w">Sep</span> <span class="n">26</span> <span class="n">13</span><span class="co">:</span><span class="n">46</span><span class="co">:</span><span class="n">39</span> <span class="n">2008</span></li><li> <span class="w">Reported</span> <span class="w">on</span> <span class="w">Fri</span> <span class="w">Sep</span> <span class="n">26</span> <span class="n">13</span><span class="co">:</span><span class="n">47</span><span class="co">:</span><span class="n">22</span> <span class="n">2008</span></li><li></li><li> <span class="w">File</span> <span class="w">wordmatch</span></li><li></li><li>  <span class="w">Subroutines</span> -- <span class="w">ordered</span> <span class="w">by</span> <span class="w">exclusive</span> <a class="l_k" href="functions/time.html">time</a></li><li> |<span class="w">Calls</span> |<span class="w">P</span>|<span class="w">F</span>|<span class="w">Inclusive</span>|<span class="w">Exclusive</span>|<span class="w">Subroutine</span>    |</li><li> |      | | |<span class="w">Time</span>     |<span class="w">Time</span>     |              |</li><li> |<span class="n">251215</span>|<span class="n">5</span>|<span class="n">1</span>|<span class="n">13.09263</span> |<span class="n">10.47692</span> |<span class="w">main::</span>|<span class="w">matches</span>|</li><li> |<span class="n">260642</span>|<span class="n">2</span>|<span class="n">1</span>|<span class="n">2.71199</span>  |<span class="n">2.71199</span>  |<span class="w">main::</span>|<span class="w">debug</span>  |</li><li> |<span class="n">1</span>     |<span class="n">1</span>|<span class="n">1</span>|<span class="n">0.21404</span>  |<span class="n">0.21404</span>  |<span class="w">main::</span>|<span class="w">report</span> |</li><li> |<span class="n">0</span>     |<span class="n">0</span>|<span class="n">0</span>|<span class="n">0</span>        |<span class="n">0</span>        |<span class="w">main::</span>|<a class="l_k" href="functions/BEGIN.html">BEGIN</a>  |</li><li></li><li></li><li> |<span class="w">Line</span>|<span class="w">Stmts</span>.|<span class="w">Exclusive</span>|<span class="w">Avg</span>.   |<span class="w">Code</span>                                           |</li><li> |    |      |<span class="w">Time</span>     |       |                                               |</li><li> |<span class="n">1</span>   |      |         |       |<span class="c">#!/usr/bin/perl                                |</span></li><li> |<span class="n">2</span>   |      |         |       |                                               |</li><li> |    |      |         |       |<a class="l_k" href="functions/use.html">use</a> <span class="w">strict</span><span class="sc">;</span>                                    |</li><li> |<span class="n">3</span>   |<span class="n">3</span>     |<span class="n">0.00086</span>  |<span class="n">0.00029</span>|<span class="c"># spent 0.00003s making 1 calls to strict::    |</span></li><li> |    |      |         |       |<span class="w">import</span>                                         |</li><li> |    |      |         |       |<a class="l_k" href="functions/use.html">use</a> <span class="w">warnings</span><span class="sc">;</span>                                  |</li><li> |<span class="n">4</span>   |<span class="n">3</span>     |<span class="n">0.01563</span>  |<span class="n">0.00521</span>|<span class="c"># spent 0.00012s making 1 calls to warnings::  |</span></li><li> |    |      |         |       |<span class="w">import</span>                                         |</li><li> |<span class="n">5</span>   |      |         |       |                                               |</li><li> |<span class="n">6</span>   |      |         |       |=<span class="w">head1</span> <span class="w">NAME</span>                                    |</li><li> |<span class="n">7</span>   |      |         |       |                                               |</li><li> |<span class="n">8</span>   |      |         |       |<span class="w">filewords</span> - <span class="w">word</span> <span class="w">analysis</span> <span class="w">of</span> <span class="w">input</span> <span class="w">file</span>        |</li><li> <span class="q">&lt;...snip...&gt;</span></li><li> |<span class="n">62</span>  |<span class="n">1</span>     |<span class="n">0.00445</span>  |<span class="n">0.00445</span>|<a class="l_k" href="functions/print.html">print</a> <span class="i">report</span><span class="s">(</span> <span class="i">%count</span> <span class="s">)</span><span class="sc">;</span>                        |</li><li> |    |      |         |       |<span class="c"># spent 0.21404s making 1 calls to main::report|</span></li><li> |<span class="n">63</span>  |      |         |       |                                               |</li><li> |    |      |         |       |<span class="c"># spent 23.56955s (10.47692+2.61571) within    |</span></li><li> |    |      |         |       |<span class="w">main::matches</span> <span class="w">which</span> <span class="w">was</span> <span class="w">called</span> <span class="n">251215</span> <a class="l_k" href="functions/times.html">times</a><span class="cm">,</span>   |</li><li> |    |      |         |       |<span class="w">avg</span> <span class="n">0.00005</span><span class="q">s/call: # 50243 times               |</span></li><li> <span class="q"> |    |      |         |       |(2.12134+0.51939s) at line 57 of wordmatch, avg|</span></li><li> <span class="q"> |    |      |         |       |0.00005s/call # 50243 times (2.17735+0.54550s) |</span></li><li> <span class="q"> |64  |      |         |       |at line 56 of wordmatch, avg 0.00005s/call</span> <span class="c">#   |</span></li><li> |    |      |         |       |<span class="n">50243</span> <a class="l_k" href="functions/times.html">times</a> <span class="s">(</span><span class="n">2.10992</span>+<span class="n">0.51797</span><span class="q">s) at line 58 of   |</span></li><li> <span class="q"> |    |      |         |       |wordmatch, avg 0.00005s/call # 50243 times     |</span></li><li> <span class="q"> |    |      |         |       |(2.12696+0.51598s) at line 55 of wordmatch, avg|</span></li><li> <span class="q"> |    |      |         |       |0.00005s/call # 50243 times (1.94134+0.51687s)</span> |</li><li> |    |      |         |       |<span class="w">at</span> <span class="w">line</span> <span class="n">54</span> <span class="w">of</span> <span class="w">wordmatch</span><span class="cm">,</span> <span class="w">avg</span> <span class="n">0.00005</span><span class="q">s/call     |</span></li><li> <span class="q"> |    |      |         |       |sub matches {                                  |</span></li><li> <span class="q"> &lt;...snip...&gt;</span></li><li> <span class="q"> |102 |      |         |       |                                               |</span></li><li> <span class="q"> |    |      |         |       |# spent 2.71199s within main::debug which was  |</span></li><li> <span class="q"> |    |      |         |       |called 260642 times, avg 0.00001s/call: #      |</span></li><li> <span class="q"> |    |      |         |       |251215 times (2.61571+0s) by main::matches at  |</span></li><li> <span class="q"> |103 |      |         |       |line 74 of wordmatch, avg 0.00001s/call</span> <span class="c"># 9427 |</span></li><li> |    |      |         |       |<a class="l_k" href="functions/times.html">times</a> <span class="s">(</span><span class="n">0.09628</span>+<span class="n">0</span><span class="q">s) at line 50 of wordmatch, avg|</span></li><li> <span class="q"> |    |      |         |       |0.00001s/call                                  |</span></li><li> <span class="q"> |    |      |         |       |sub debug {                                    |</span></li><li> <span class="q"> |104 |260642|0.58496  |2e-06  |my $message = shift;                           |</span></li><li> <span class="q"> |105 |      |         |       |                                               |</span></li><li> <span class="q"> |106 |260642|1.09917  |4e-06  |if ( $debug ) {                                |</span></li><li> <span class="q"> |107 |      |         |       |print STDERR &quot;DBG: $message\n&quot;;                |</span></li><li> <span class="q"> |108 |      |         |       |}                                              |</span></li><li> <span class="q"> |109 |      |         |       |}                                              |</span></li><li> <span class="q"> |110 |      |         |       |                                               |</span></li><li> <span class="q"> |111 |1     |0.01501  |0.01501|exit 0;                                        |</span></li><li> <span class="q"> |112 |      |         |       |                                               |</span></li></ol></pre><p>Oodles of very useful information in there - this seems to be the way forward.</p>
<p>See also <code class="inline"><a href="http://search.cpan.org/perldoc/Devel::NYTProf::Apache">Devel::NYTProf::Apache</a></code> which hooks <code class="inline"><span class="w">Devel::NYTProf</span></code>
 into
<code class="inline"><span class="w">mod_perl</span></code>
687 688 689 690 691 692
.</p>
<a name="SORTING"></a><h1>SORTING</h1>
<p>Perl modules are not the only tools a performance analyst has at their
disposal, system tools like <code class="inline"><a class="l_k" href="functions/time.html">time</a></code> should not be overlooked as the next
example shows, where we take a quick look at sorting.  Many books, theses and
articles, have been written about efficient sorting algorithms, and this is not
693
the place to repeat such work, there's several good sorting modules which
694 695 696
deserve taking a look at too: <code class="inline"><span class="w">Sort::Maker</span></code>
, <code class="inline"><span class="w">Sort::Key</span></code>
 spring to mind.
697
However, it's still possible to make some observations on certain Perl specific
698 699 700 701 702
interpretations on issues relating to sorting data sets and give an example or
two with regard to how sorting large data volumes can effect performance.
Firstly, an often overlooked point when sorting large amounts of data, one can
attempt to reduce the data set to be dealt with and in many cases <code class="inline"><a class="l_k" href="functions/grep.html">grep()</a></code> can
be quite useful as a simple filter:</p>
703
<pre class="verbatim"><ol><li> <span class="i">@data</span> = <a class="l_k" href="functions/sort.html">sort</a> <a class="l_k" href="functions/grep.html">grep</a> <span class="s">{</span> <span class="q">/$filter/</span> <span class="s">}</span> <span class="i">@incoming</span></li></ol></pre><p>A command such as this can vastly reduce the volume of material to actually
704
sort through in the first place, and should not be too lightly disregarded
705
purely on the basis of its simplicity.  The <code class="inline"><span class="w">KISS</span></code>
706 707
 principle is too often
overlooked - the next example uses the simple system <code class="inline"><a class="l_k" href="functions/time.html">time</a></code> utility to
708
demonstrate.  Let's take a look at an actual example of sorting the contents of
709 710 711
a large file, an apache logfile would do.  This one has over a quarter of a
million lines, is 50M in size, and a snippet of it looks like this:</p>
<p># logfile</p>
712
<pre class="verbatim"><ol><li> <span class="n">188.209</span>-<span class="n">65</span>-<span class="n">87.</span><span class="w">adsl</span>-<span class="w">dyn</span>.<span class="w">isp</span>.<span class="w">belgacom</span>.<span class="w">be</span> - - <span class="s">[</span><span class="n">08</span>/<span class="w">Feb</span>/<span class="n">2007</span><span class="co">:</span><span class="n">12</span><span class="co">:</span><span class="n">57</span><span class="co">:</span><span class="n">16</span> +<span class="n">0000</span><span class="s">]</span> <span class="q">&quot;GET /favicon.ico HTTP/1.1&quot;</span> <span class="n">404</span> <span class="n">209</span> <span class="q">&quot;-&quot;</span> <span class="q">&quot;Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)&quot;</span></li><li> <span class="n">188.209</span>-<span class="n">65</span>-<span class="n">87.</span><span class="w">adsl</span>-<span class="w">dyn</span>.<span class="w">isp</span>.<span class="w">belgacom</span>.<span class="w">be</span> - - <span class="s">[</span><span class="n">08</span>/<span class="w">Feb</span>/<span class="n">2007</span><span class="co">:</span><span class="n">12</span><span class="co">:</span><span class="n">57</span><span class="co">:</span><span class="n">16</span> +<span class="n">0000</span><span class="s">]</span> <span class="q">&quot;GET /favicon.ico HTTP/1.1&quot;</span> <span class="n">404</span> <span class="n">209</span> <span class="q">&quot;-&quot;</span> <span class="q">&quot;Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)&quot;</span></li><li> <span class="v">151.56.71.198</span> - - <span class="s">[</span><span class="n">08</span>/<span class="w">Feb</span>/<span class="n">2007</span><span class="co">:</span><span class="n">12</span><span class="co">:</span><span class="n">57</span><span class="co">:</span><span class="n">41</span> +<span class="n">0000</span><span class="s">]</span> <span class="q">&quot;GET /suse-on-vaio.html HTTP/1.1&quot;</span> <span class="n">200</span> <span class="n">2858</span> <span class="q">&quot;<a href="http://www.linux-on-laptops.com/sony.html">http://www.linux-on-laptops.com/sony.html</a>&quot;</span> <span class="q">&quot;Mozilla/5.0 (Windows; U; Windows NT 5.2; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1&quot;</span></li><li> <span class="v">151.56.71.198</span> - - <span class="s">[</span><span class="n">08</span>/<span class="w">Feb</span>/<span class="n">2007</span><span class="co">:</span><span class="n">12</span><span class="co">:</span><span class="n">57</span><span class="co">:</span><span class="n">42</span> +<span class="n">0000</span><span class="s">]</span> <span class="q">&quot;GET /data/css HTTP/1.1&quot;</span> <span class="n">404</span> <span class="n">206</span> <span class="q">&quot;<a href="http://www.rfi.net/suse-on-vaio.html">http://www.rfi.net/suse-on-vaio.html</a>&quot;</span> <span class="q">&quot;Mozilla/5.0 (Windows; U; Windows NT 5.2; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1&quot;</span></li><li> <span class="v">151.56.71.198</span> - - <span class="s">[</span><span class="n">08</span>/<span class="w">Feb</span>/<span class="n">2007</span><span class="co">:</span><span class="n">12</span><span class="co">:</span><span class="n">57</span><span class="co">:</span><span class="n">43</span> +<span class="n">0000</span><span class="s">]</span> <span class="q">&quot;GET /favicon.ico HTTP/1.1&quot;</span> <span class="n">404</span> <span class="n">209</span> <span class="q">&quot;-&quot;</span> <span class="q">&quot;Mozilla/5.0 (Windows; U; Windows NT 5.2; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1&quot;</span></li><li> <span class="v">217.113.68.60</span> - - <span class="s">[</span><span class="n">08</span>/<span class="w">Feb</span>/<span class="n">2007</span><span class="co">:</span><span class="n">13</span><span class="co">:</span><span class="n">02</span><span class="co">:</span><span class="n">15</span> +<span class="n">0000</span><span class="s">]</span> <span class="q">&quot;GET / HTTP/1.1&quot;</span> <span class="n">304</span> - <span class="q">&quot;-&quot;</span> <span class="q">&quot;Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)&quot;</span></li><li> <span class="v">217.113.68.60</span> - - <span class="s">[</span><span class="n">08</span>/<span class="w">Feb</span>/<span class="n">2007</span><span class="co">:</span><span class="n">13</span><span class="co">:</span><span class="n">02</span><span class="co">:</span><span class="n">16</span> +<span class="n">0000</span><span class="s">]</span> <span class="q">&quot;GET /data/css HTTP/1.1&quot;</span> <span class="n">404</span> <span class="n">206</span> <span class="q">&quot;<a href="http://www.rfi.net/">http://www.rfi.net/</a>&quot;</span> <span class="q">&quot;Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)&quot;</span></li><li> <span class="w">debora</span>.<span class="w">to</span>.<span class="w">isac</span>.<span class="w">cnr</span>.<span class="w">it</span> - - <span class="s">[</span><span class="n">08</span>/<span class="w">Feb</span>/<span class="n">2007</span><span class="co">:</span><span class="n">13</span><span class="co">:</span><span class="n">03</span><span class="co">:</span><span class="n">58</span> +<span class="n">0000</span><span class="s">]</span> <span class="q">&quot;GET /suse-on-vaio.html HTTP/1.1&quot;</span> <span class="n">200</span> <span class="n">2858</span> <span class="q">&quot;<a href="http://www.linux-on-laptops.com/sony.html">http://www.linux-on-laptops.com/sony.html</a>&quot;</span> <span class="q">&quot;Mozilla/5.0 (compatible; Konqueror/3.4; Linux) KHTML/3.4.0 (like Gecko)&quot;</span></li><li> <span class="w">debora</span>.<span class="w">to</span>.<span class="w">isac</span>.<span class="w">cnr</span>.<span class="w">it</span> - - <span class="s">[</span><span class="n">08</span>/<span class="w">Feb</span>/<span class="n">2007</span><span class="co">:</span><span class="n">13</span><span class="co">:</span><span class="n">03</span><span class="co">:</span><span class="n">58</span> +<span class="n">0000</span><span class="s">]</span> <span class="q">&quot;GET /data/css HTTP/1.1&quot;</span> <span class="n">404</span> <span class="n">206</span> <span class="q">&quot;<a href="http://www.rfi.net/suse-on-vaio.html">http://www.rfi.net/suse-on-vaio.html</a>&quot;</span> <span class="q">&quot;Mozilla/5.0 (compatible; Konqueror/3.4; Linux) KHTML/3.4.0 (like Gecko)&quot;</span></li><li> <span class="w">debora</span>.<span class="w">to</span>.<span class="w">isac</span>.<span class="w">cnr</span>.<span class="w">it</span> - - <span class="s">[</span><span class="n">08</span>/<span class="w">Feb</span>/<span class="n">2007</span><span class="co">:</span><span class="n">13</span><span class="co">:</span><span class="n">03</span><span class="co">:</span><span class="n">58</span> +<span class="n">0000</span><span class="s">]</span> <span class="q">&quot;GET /favicon.ico HTTP/1.1&quot;</span> <span class="n">404</span> <span class="n">209</span> <span class="q">&quot;-&quot;</span> <span class="q">&quot;Mozilla/5.0 (compatible; Konqueror/3.4; Linux) KHTML/3.4.0 (like Gecko)&quot;</span></li><li> <span class="v">195.24.196.99</span> - - <span class="s">[</span><span class="n">08</span>/<span class="w">Feb</span>/<span class="n">2007</span><span class="co">:</span><span class="n">13</span><span class="co">:</span><span class="n">26</span><span class="co">:</span><span class="n">48</span> +<span class="n">0000</span><span class="s">]</span> <span class="q">&quot;GET / HTTP/1.0&quot;</span> <span class="n">200</span> <span class="n">3309</span> <span class="q">&quot;-&quot;</span> <span class="q">&quot;Mozilla/5.0 (Windows; U; Windows NT 5.1; fr; rv:1.8.0.9) Gecko/20061206 Firefox/1.5.0.9&quot;</span></li><li> <span class="v">195.24.196.99</span> - - <span class="s">[</span><span class="n">08</span>/<span class="w">Feb</span>/<span class="n">2007</span><span class="co">:</span><span class="n">13</span><span class="co">:</span><span class="n">26</span><span class="co">:</span><span class="n">58</span> +<span class="n">0000</span><span class="s">]</span> <span class="q">&quot;GET /data/css HTTP/1.0&quot;</span> <span class="n">404</span> <span class="n">206</span> <span class="q">&quot;<a href="http://www.rfi.net/">http://www.rfi.net/</a>&quot;</span> <span class="q">&quot;Mozilla/5.0 (Windows; U; Windows NT 5.1; fr; rv:1.8.0.9) Gecko/20061206 Firefox/1.5.0.9&quot;</span></li><li> <span class="v">195.24.196.99</span> - - <span class="s">[</span><span class="n">08</span>/<span class="w">Feb</span>/<span class="n">2007</span><span class="co">:</span><span class="n">13</span><span class="co">:</span><span class="n">26</span><span class="co">:</span><span class="n">59</span> +<span class="n">0000</span><span class="s">]</span> <span class="q">&quot;GET /favicon.ico HTTP/1.0&quot;</span> <span class="n">404</span> <span class="n">209</span> <span class="q">&quot;-&quot;</span> <span class="q">&quot;Mozilla/5.0 (Windows; U; Windows NT 5.1; fr; rv:1.8.0.9) Gecko/20061206 Firefox/1.5.0.9&quot;</span></li><li> <span class="w">crawl1</span>.<span class="w">cosmixcorp</span>.<span class="w">com</span> - - <span class="s">[</span><span class="n">08</span>/<span class="w">Feb</span>/<span class="n">2007</span><span class="co">:</span><span class="n">13</span><span class="co">:</span><span class="n">27</span><span class="co">:</span><span class="n">57</span> +<span class="n">0000</span><span class="s">]</span> <span class="q">&quot;GET /robots.txt HTTP/1.0&quot;</span> <span class="n">200</span> <span class="n">179</span> <span class="q">&quot;-&quot;</span> <span class="q">&quot;voyager/1.0&quot;</span></li><li> <span class="w">crawl1</span>.<span class="w">cosmixcorp</span>.<span class="w">com</span> - - <span class="s">[</span><span class="n">08</span>/<span class="w">Feb</span>/<span class="n">2007</span><span class="co">:</span><span class="n">13</span><span class="co">:</span><span class="n">28</span><span class="co">:</span><span class="n">25</span> +<span class="n">0000</span><span class="s">]</span> <span class="q">&quot;GET /links.html HTTP/1.0&quot;</span> <span class="n">200</span> <span class="n">3413</span> <span class="q">&quot;-&quot;</span> <span class="q">&quot;voyager/1.0&quot;</span></li><li> <span class="w">fhm226</span>.<span class="w">internetdsl</span>.<span class="w">tpnet</span>.<span class="w">pl</span> - - <span class="s">[</span><span class="n">08</span>/<span class="w">Feb</span>/<span class="n">2007</span><span class="co">:</span><span class="n">13</span><span class="co">:</span><span class="n">37</span><span class="co">:</span><span class="n">32</span> +<span class="n">0000</span><span class="s">]</span> <span class="q">&quot;GET /suse-on-vaio.html HTTP/1.1&quot;</span> <span class="n">200</span> <span class="n">2858</span> <span class="q">&quot;<a href="http://www.linux-on-laptops.com/sony.html">http://www.linux-on-laptops.com/sony.html</a>&quot;</span> <span class="q">&quot;Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)&quot;</span></li><li> <span class="w">fhm226</span>.<span class="w">internetdsl</span>.<span class="w">tpnet</span>.<span class="w">pl</span> - - <span class="s">[</span><span class="n">08</span>/<span class="w">Feb</span>/<span class="n">2007</span><span class="co">:</span><span class="n">13</span><span class="co">:</span><span class="n">37</span><span class="co">:</span><span class="n">34</span> +<span class="n">0000</span><span class="s">]</span> <span class="q">&quot;GET /data/css HTTP/1.1&quot;</span> <span class="n">404</span> <span class="n">206</span> <span class="q">&quot;<a href="http://www.rfi.net/suse-on-vaio.html">http://www.rfi.net/suse-on-vaio.html</a>&quot;</span> <span class="q">&quot;Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)&quot;</span></li><li> <span class="v">80.247.140.134</span> - - <span class="s">[</span><span class="n">08</span>/<span class="w">Feb</span>/<span class="n">2007</span><span class="co">:</span><span class="n">13</span><span class="co">:</span><span class="n">57</span><span class="co">:</span><span class="n">35</span> +<span class="n">0000</span><span class="s">]</span> <span class="q">&quot;GET / HTTP/1.1&quot;</span> <span class="n">200</span> <span class="n">3309</span> <span class="q">&quot;-&quot;</span> <span class="q">&quot;Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322)&quot;</span></li><li> <span class="v">80.247.140.134</span> - - <span class="s">[</span><span class="n">08</span>/<span class="w">Feb</span>/<span class="n">2007</span><span class="co">:</span><span class="n">13</span><span class="co">:</span><span class="n">57</span><span class="co">:</span><span class="n">37</span> +<span class="n">0000</span><span class="s">]</span> <span class="q">&quot;GET /data/css HTTP/1.1&quot;</span> <span class="n">404</span> <span class="n">206</span> <span class="q">&quot;<a href="http://www.rfi.net">http://www.rfi.net</a>&quot;</span> <span class="q">&quot;Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322)&quot;</span></li><li> <a class="l_k" href="functions/pop.html">pop</a>.<span class="w">compuscan</span>.<span class="w">co</span>.<span class="w">za</span> - - <span class="s">[</span><span class="n">08</span>/<span class="w">Feb</span>/<span class="n">2007</span><span class="co">:</span><span class="n">14</span><span class="co">:</span><span class="n">10</span><span class="co">:</span><span class="n">43</span> +<span class="n">0000</span><span class="s">]</span> <span class="q">&quot;GET / HTTP/1.1&quot;</span> <span class="n">200</span> <span class="n">3309</span> <span class="q">&quot;-&quot;</span> <span class="q">&quot;www.clamav.net&quot;</span></li><li> <span class="w">livebot</span>-<span class="n">207</span>-<span class="n">46</span>-<span class="n">98</span>-<span class="n">57.</span><span class="w">search</span>.<span class="w">live</span>.<span class="w">com</span> - - <span class="s">[</span><span class="n">08</span>/<span class="w">Feb</span>/<span class="n">2007</span><span class="co">:</span><span class="n">14</span><span class="co">:</span><span class="n">12</span><span class="co">:</span><span class="n">04</span> +<span class="n">0000</span><span class="s">]</span> <span class="q">&quot;GET /robots.txt HTTP/1.0&quot;</span> <span class="n">200</span> <span class="n">179</span> <span class="q">&quot;-&quot;</span> <span class="q">&quot;msnbot/1.0 (+<a href="http://search.msn.com/msnbot.htm">http://search.msn.com/msnbot.htm</a>)&quot;</span></li><li> <span class="w">livebot</span>-<span class="n">207</span>-<span class="n">46</span>-<span class="n">98</span>-<span class="n">57.</span><span class="w">search</span>.<span class="w">live</span>.<span class="w">com</span> - - <span class="s">[</span><span class="n">08</span>/<span class="w">Feb</span>/<span class="n">2007</span><span class="co">:</span><span class="n">14</span><span class="co">:</span><span class="n">12</span><span class="co">:</span><span class="n">04</span> +<span class="n">0000</span><span class="s">]</span> <span class="q">&quot;GET /html/oracle.html HTTP/1.0&quot;</span> <span class="n">404</span> <span class="n">214</span> <span class="q">&quot;-&quot;</span> <span class="q">&quot;msnbot/1.0 (+<a href="http://search.msn.com/msnbot.htm">http://search.msn.com/msnbot.htm</a>)&quot;</span></li><li> <span class="w">dslb</span>-<span class="n">088</span>-<span class="n">064</span>-<span class="n">005</span>-<span class="n">154.</span><span class="w">pools</span>.<span class="w">arcor</span>-<span class="w">ip</span>.<span class="w">net</span> - - <span class="s">[</span><span class="n">08</span>/<span class="w">Feb</span>/<span class="n">2007</span><span class="co">:</span><span class="n">14</span><span class="co">:</span><span class="n">12</span><span class="co">:</span><span class="n">15</span> +<span class="n">0000</span><span class="s">]</span> <span class="q">&quot;GET / HTTP/1.1&quot;</span> <span class="n">200</span> <span class="n">3309</span> <span class="q">&quot;-&quot;</span> <span class="q">&quot;www.clamav.net&quot;</span></li><li> <span class="v">196.201.92.41</span> - - <span class="s">[</span><span class="n">08</span>/<span class="w">Feb</span>/<span class="n">2007</span><span class="co">:</span><span class="n">14</span><span class="co">:</span><span class="n">15</span><span class="co">:</span><span class="n">01</span> +<span class="n">0000</span><span class="s">]</span> <span class="q">&quot;GET / HTTP/1.1&quot;</span> <span class="n">200</span> <span class="n">3309</span> <span class="q">&quot;-&quot;</span> <span class="q">&quot;MOT-L7/08.B7.DCR MIB/2.2.1 Profile/MIDP-2.0 Configuration/CLDC-1.1&quot;</span></li></ol></pre><p>The specific task here is to sort the 286,525 lines of this file by Response
713 714 715 716
Code, Query, Browser, Referring Url, and lastly Date.  One solution might be to
use the following code, which iterates over the files given on the
command-line.</p>
<p># sort-apache-log</p>
717
<pre class="verbatim"><ol><li> <span class="c">#!/usr/bin/perl -n</span></li><li></li><li> <a class="l_k" href="functions/use.html">use</a> <span class="w">strict</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/use.html">use</a> <span class="w">warnings</span><span class="sc">;</span></li><li></li><li> <a class="l_k" href="functions/my.html">my</a> <span class="i">@data</span><span class="sc">;</span></li><li></li><li> <span class="j">LINE:</span></li><li> <a class="l_k" href="functions/while.html">while</a> <span class="s">(</span> &lt;&gt; <span class="s">)</span> <span class="s">{</span></li><li>     <a class="l_k" href="functions/my.html">my</a> <span class="i">$line</span> = <span class="i">$_</span><span class="sc">;</span></li><li>     <a class="l_k" href="functions/if.html">if</a> <span class="s">(</span></li><li>         <span class="i">$line</span> =~ <span class="q">m/^(</span></li><li>             <span class="q">             ([\w\.\-]+)             # client</span></li><li>             <span class="q">             \s*-\s*-\s*\[</span></li><li>             <span class="q">             ([^]]+)                 # date</span></li><li>             <span class="q">             \]\s*&quot;\w+\s*</span></li><li>             <span class="q">             (\S+)                   # query</span></li><li>             <span class="q">             [^&quot;]+&quot;\s*</span></li><li>             <span class="q">             (\d+)                   # status</span></li><li>             <span class="q">             \s+\S+\s+&quot;[^&quot;]*&quot;\s+&quot;</span></li><li>             <span class="q">             ([^&quot;]*)                 # browser</span></li><li>             <span class="q">             &quot;</span></li><li>             <span class="q">             .*</span></li><li>         <span class="q">         )$/x</span></li><li>     <span class="s">)</span> <span class="s">{</span></li><li>         <a class="l_k" href="functions/my.html">my</a> <span class="i">@chunks</span> = <a class="l_k" href="functions/split.html">split</a><span class="s">(</span><span class="q">/ +/</span><span class="cm">,</span> <span class="i">$line</span><span class="s">)</span><span class="sc">;</span></li><li>         <a class="l_k" href="functions/my.html">my</a> <span class="i">$ip</span>      = <span class="i">$1</span><span class="sc">;</span></li><li>         <a class="l_k" href="functions/my.html">my</a> <span class="i">$date</span>    = <span class="i">$2</span><span class="sc">;</span></li><li>         <a class="l_k" href="functions/my.html">my</a> <span class="i">$query</span>   = <span class="i">$3</span><span class="sc">;</span></li><li>         <a class="l_k" href="functions/my.html">my</a> <span class="i">$status</span>  = <span class="i">$4</span><span class="sc">;</span></li><li>         <a class="l_k" href="functions/my.html">my</a> <span class="i">$browser</span> = <span class="i">$5</span><span class="sc">;</span></li><li></li><li>         <a class="l_k" href="functions/push.html">push</a><span class="s">(</span><span class="i">@data</span><span class="cm">,</span> <span class="s">[</span><span class="i">$ip</span><span class="cm">,</span> <span class="i">$date</span><span class="cm">,</span> <span class="i">$query</span><span class="cm">,</span> <span class="i">$status</span><span class="cm">,</span> <span class="i">$browser</span><span class="cm">,</span> <span class="i">$line</span><span class="s">]</span><span class="s">)</span><span class="sc">;</span></li><li>     <span class="s">}</span></li><li> <span class="s">}</span></li><li></li><li> <a class="l_k" href="functions/my.html">my</a> <span class="i">@sorted</span> = <a class="l_k" href="functions/sort.html">sort</a> <span class="s">{</span></li><li>     <span class="i">$a</span>-&gt;[<span class="n">3</span>] <a class="l_k" href="functions/cmp.html">cmp</a> <span class="i">$b</span>-&gt;[<span class="n">3</span>]</li><li>             ||</li><li>     <span class="i">$a</span>-&gt;[<span class="n">2</span>] <a class="l_k" href="functions/cmp.html">cmp</a> <span class="i">$b</span>-&gt;[<span class="n">2</span>]</li><li>             ||</li><li>     <span class="i">$a</span>-&gt;[<span class="n">0</span>] <a class="l_k" href="functions/cmp.html">cmp</a> <span class="i">$b</span>-&gt;[<span class="n">0</span>]</li><li>             ||</li><li>     <span class="i">$a</span>-&gt;[<span class="n">1</span>] <a class="l_k" href="functions/cmp.html">cmp</a> <span class="i">$b</span>-&gt;[<span class="n">1</span>]</li><li>             ||</li><li>     <span class="i">$a</span>-&gt;[<span class="n">4</span>] <a class="l_k" href="functions/cmp.html">cmp</a> <span class="i">$b</span>-&gt;[<span class="n">4</span>]</li><li> <span class="s">}</span> <span class="i">@data</span><span class="sc">;</span></li><li></li><li> <a class="l_k" href="functions/foreach.html">foreach</a> <a class="l_k" href="functions/my.html">my</a> <span class="i">$data</span> <span class="s">(</span> <span class="i">@sorted</span> <span class="s">)</span> <span class="s">{</span></li><li>     <a class="l_k" href="functions/print.html">print</a> <span class="i">$data</span>-&gt;[<span class="n">5</span>]<span class="sc">;</span></li><li> <span class="s">}</span></li><li></li><li> <a class="l_k" href="functions/exit.html">exit</a> <span class="n">0</span><span class="sc">;</span></li></ol></pre><p>When running this program, redirect <code class="inline"><span class="w">STDOUT</span></code>
718 719 720
 so it is possible to check the
output is correct from following test runs and use the system <code class="inline"><a class="l_k" href="functions/time.html">time</a></code> utility
to check the overall runtime.</p>
721
<pre class="verbatim"><ol><li> <span class="i">$&gt;</span> <a class="l_k" href="functions/time.html">time</a> .<span class="q">/sort-apache-log logfile &gt; out-sort</span></li><li></li><li> <span class="q"> real    0m17.371s</span></li><li> <span class="q"> user    0m15.757s</span></li><li> <span class="q"> sys     0m0.592s</span></li></ol></pre><p>The program took just over 17 wallclock seconds to run.  Note the different
722
values <code class="inline"><a class="l_k" href="functions/time.html">time</a></code> outputs, it's important to always use the same one, and to not
723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744
confuse what each one means.</p>
<ul>
<li><a name="Elapsed-Real-Time"></a><b>Elapsed Real Time</b>
<p>The overall, or wallclock, time between when <code class="inline"><a class="l_k" href="functions/time.html">time</a></code> was called, and when it
terminates.  The elapsed time includes both user and system times, and time
spent waiting for other users and processes on the system.  Inevitably, this is
the most approximate of the measurements given.</p>
</li>
<li><a name="User-CPU-Time"></a><b>User CPU Time</b>
<p>The user time is the amount of time the entire process spent on behalf of the
user on this system executing this program.</p>
</li>
<li><a name="System-CPU-Time"></a><b>System CPU Time</b>
<p>The system time is the amount of time the kernel itself spent executing
routines, or system calls, on behalf of this process user.</p>
</li>
</ul>
<p>Running this same process as a <code class="inline"><span class="w">Schwarzian</span> <span class="w">Transform</span></code>
 it is possible to
eliminate the input and output arrays for storing all the data, and work on the
input directly as it arrives too.  Otherwise, the code looks fairly similar:</p>
<p># sort-apache-log-schwarzian</p>
745 746
<pre class="verbatim"><ol><li> <span class="c">#!/usr/bin/perl -n</span></li><li></li><li> <a class="l_k" href="functions/use.html">use</a> <span class="w">strict</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/use.html">use</a> <span class="w">warnings</span><span class="sc">;</span></li><li></li><li> <a class="l_k" href="functions/print.html">print</a></li><li></li><li>     <a class="l_k" href="functions/map.html">map</a> <span class="i">$_</span>-&gt;[<span class="n">0</span>] <span class="cm">=&gt;</span></li><li></li><li>     <a class="l_k" href="functions/sort.html">sort</a> <span class="s">{</span></li><li>         <span class="i">$a</span>-&gt;[<span class="n">4</span>] <a class="l_k" href="functions/cmp.html">cmp</a> <span class="i">$b</span>-&gt;[<span class="n">4</span>]</li><li>                 ||</li><li>         <span class="i">$a</span>-&gt;[<span class="n">3</span>] <a class="l_k" href="functions/cmp.html">cmp</a> <span class="i">$b</span>-&gt;[<span class="n">3</span>]</li><li>                 ||</li><li>         <span class="i">$a</span>-&gt;[<span class="n">1</span>] <a class="l_k" href="functions/cmp.html">cmp</a> <span class="i">$b</span>-&gt;[<span class="n">1</span>]</li><li>                 ||</li><li>         <span class="i">$a</span>-&gt;[<span class="n">2</span>] <a class="l_k" href="functions/cmp.html">cmp</a> <span class="i">$b</span>-&gt;[<span class="n">2</span>]</li><li>                 ||</li><li>         <span class="i">$a</span>-&gt;[<span class="n">5</span>] <a class="l_k" href="functions/cmp.html">cmp</a> <span class="i">$b</span>-&gt;[<span class="n">5</span>]</li><li>     <span class="s">}</span></li><li>     <a class="l_k" href="functions/map.html">map</a>  <span class="s">[</span> <span class="i">$_</span><span class="cm">,</span> <span class="q">m/^(</span></li><li>         <span class="q">         ([\w\.\-]+)             # client</span></li><li>         <span class="q">         \s*-\s*-\s*\[</span></li><li>         <span class="q">         ([^]]+)                 # date</span></li><li>         <span class="q">         \]\s*&quot;\w+\s*</span></li><li>         <span class="q">         (\S+)                   # query</span></li><li>         <span class="q">         [^&quot;]+&quot;\s*</span></li><li>         <span class="q">         (\d+)                   # status</span></li><li>         <span class="q">         \s+\S+\s+&quot;[^&quot;]*&quot;\s+&quot;</span></li><li>         <span class="q">         ([^&quot;]*)                 # browser</span></li><li>         <span class="q">         &quot;</span></li><li>         <span class="q">         .*</span></li><li>     <span class="q">     )$/xo</span> <span class="s">]</span></li><li></li><li>     <span class="cm">=&gt;</span> &lt;&gt;<span class="sc">;</span></li><li></li><li> <a class="l_k" href="functions/exit.html">exit</a> <span class="n">0</span><span class="sc">;</span></li></ol></pre><p>Run the new code against the same logfile, as above, to check the new time.</p>
<pre class="verbatim"><ol><li> <span class="i">$&gt;</span> <a class="l_k" href="functions/time.html">time</a> .<span class="q">/sort-apache-log-schwarzian logfile &gt; out-schwarz</span></li><li></li><li> <span class="q"> real    0m9.664s</span></li><li> <span class="q"> user    0m8.873s</span></li><li> <span class="q"> sys     0m0.704s</span></li></ol></pre><p>The time has been cut in half, which is a respectable speed improvement by any
747
standard.  Naturally, it is important to check the output is consistent with
748
the first program run, this is where the Unix system <code class="inline"><span class="w">cksum</span></code>
749
 utility comes in.</p>
750
<pre class="verbatim"><ol><li> <span class="i">$&gt;</span> <span class="w">cksum</span> <span class="w">out</span>-<a class="l_k" href="functions/sort.html">sort</a> <span class="i">out</span>-<span class="w">schwarz</span></li><li> <span class="n">3044173777</span> <span class="n">52029194</span> <span class="w">out</span>-<a class="l_k" href="functions/sort.html">sort</a></li><li> <span class="n">3044173777</span> <span class="n">52029194</span> <span class="w">out</span>-<span class="w">schwarz</span></li></ol></pre><p>BTW. Beware too of pressure from managers who see you speed a program up by 50%
751
of the runtime once, only to get a request one month later to do the same again
752
(true story) - you'll just have to point out you're only human, even if you are a
753
Perl programmer, and you'll see what you can do...</p>
754 755 756 757 758 759 760
<a name="LOGGING"></a><h1>LOGGING</h1>
<p>An essential part of any good development process is appropriate error handling
with appropriately informative messages, however there exists a school of
thought which suggests that log files should be <i>chatty</i>, as if the chain of
unbroken output somehow ensures the survival of the program.  If speed is in
any way an issue, this approach is wrong.</p>
<p>A common sight is code which looks something like this:</p>
761
<pre class="verbatim"><ol><li> <span class="w">logger</span><span class="w">-&gt;debug</span><span class="s">(</span> <span class="q">&quot;A logging message via process-id: $$ INC: &quot;</span></li><li>                                                       . <span class="i">Dumper</span><span class="s">(</span>\<span class="i">%INC</span><span class="s">)</span> <span class="s">)</span></li></ol></pre><p>The problem is that this code will always be parsed and executed, even when the
762 763 764 765
debug level set in the logging configuration file is zero.  Once the debug()
subroutine has been entered, and the internal <code class="inline"><span class="i">$debug</span></code>
 variable confirmed to
be zero, for example, the message which has been sent in will be discarded and
766 767
the program will continue.  In the example given though, the <code class="inline">\<span class="i">%INC</span></code>
 hash will
768 769
already have been dumped, and the message string constructed, all of which work
could be bypassed by a debug variable at the statement level, like this:</p>
770
<pre class="verbatim"><ol><li> <span class="w">logger</span><span class="w">-&gt;debug</span><span class="s">(</span> <span class="q">&quot;A logging message via process-id: $$ INC: &quot;</span></li><li>                                            . <span class="i">Dumper</span><span class="s">(</span>\<span class="i">%INC</span><span class="s">)</span> <span class="s">)</span> <a class="l_k" href="functions/if.html">if</a> <span class="i">$DEBUG</span><span class="sc">;</span></li></ol></pre><p>This effect can be demonstrated by setting up a test script with both forms,
771 772 773 774
including a <code class="inline"><span class="i">debug</span><span class="s">(</span><span class="s">)</span></code>
 subroutine to emulate typical <code class="inline"><span class="i">logger</span><span class="s">(</span><span class="s">)</span></code>
 functionality.</p>
<p># ifdebug</p>
775
<pre class="verbatim"><ol><li> <span class="c">#!/usr/bin/perl</span></li><li></li><li> <a class="l_k" href="functions/use.html">use</a> <span class="w">strict</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/use.html">use</a> <span class="w">warnings</span><span class="sc">;</span></li><li></li><li> <a class="l_k" href="functions/use.html">use</a> <span class="w">Benchmark</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/use.html">use</a> <span class="w">Data::Dumper</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/my.html">my</a> <span class="i">$DEBUG</span> = <span class="n">0</span><span class="sc">;</span></li><li></li><li><a name="debug"></a> sub <span class="m">debug</span> <span class="s">{</span></li><li>     <a class="l_k" href="functions/my.html">my</a> <span class="i">$msg</span> = <a class="l_k" href="functions/shift.html">shift</a><span class="sc">;</span></li><li></li><li>     <a class="l_k" href="functions/if.html">if</a> <span class="s">(</span> <span class="i">$DEBUG</span> <span class="s">)</span> <span class="s">{</span></li><li>         <a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;DEBUG: $msg\n&quot;</span><span class="sc">;</span></li><li>     <span class="s">}</span></li><li> <span class="s">}</span><span class="sc">;</span></li><li></li><li> <span class="i">timethese</span><span class="s">(</span><span class="n">100000</span><span class="cm">,</span> <span class="s">{</span></li><li>         <span class="q">&#39;debug&#39;</span>       <span class="cm">=&gt;</span> <a class="l_k" href="functions/sub.html">sub</a> <span class="s">{</span></li><li>             <span class="i">debug</span><span class="s">(</span> <span class="q">&quot;A $0 logging message via process-id: $$&quot;</span> . <span class="i">Dumper</span><span class="s">(</span>\<span class="i">%INC</span><span class="s">)</span> <span class="s">)</span></li><li>         <span class="s">}</span><span class="cm">,</span></li><li>         <span class="q">&#39;ifdebug&#39;</span>  <span class="cm">=&gt;</span> <a class="l_k" href="functions/sub.html">sub</a> <span class="s">{</span></li><li>             <span class="i">debug</span><span class="s">(</span> <span class="q">&quot;A $0 logging message via process-id: $$&quot;</span> . <span class="i">Dumper</span><span class="s">(</span>\<span class="i">%INC</span><span class="s">)</span> <span class="s">)</span> <a class="l_k" href="functions/if.html">if</a> <span class="i">$DEBUG</span></li><li>         <span class="s">}</span><span class="cm">,</span></li><li> <span class="s">}</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>Let's see what <code class="inline"><span class="w">Benchmark</span></code>
776
 makes of this:</p>
777
<pre class="verbatim"><ol><li> <span class="i">$&gt;</span> <span class="w">perl</span> <span class="w">ifdebug</span></li><li> <span class="w">Benchmark</span><span class="co">:</span> <span class="w">timing</span> <span class="n">100000</span> <span class="w">iterations</span> <span class="w">of</span> <span class="w">constant</span><span class="cm">,</span> <a class="l_k" href="functions/sub.html">sub</a>...</li><li>    <span class="w">ifdebug</span><span class="co">:</span>  <span class="n">0</span> <span class="w">wallclock</span> <span class="w">secs</span> <span class="s">(</span> <span class="n">0.01</span> <span class="w">usr</span> +  <span class="n">0.00</span> <span class="w">sys</span> =  <span class="n">0.01</span> <span class="w">CPU</span><span class="s">)</span> <span class="i">@ 10000000</span>.<span class="n">00</span>/<span class="q">s (n=100000)</span></li><li>             <span class="q">             (warning: too few iterations for a reliable count)</span></li><li>      <span class="w">debug</span><span class="co">:</span> <span class="n">14</span> <span class="w">wallclock</span> <span class="w">secs</span> <span class="s">(</span><span class="n">13.18</span> <span class="w">usr</span> +  <span class="n">0.04</span> <span class="w">sys</span> = <span class="n">13.22</span> <span class="w">CPU</span><span class="s">)</span> <span class="i">@ 7564</span>.<span class="n">30</span>/<span class="q">s (n=100000)</span></li></ol></pre><p>In the one case the code, which does exactly the same thing as far as
778 779 780 781 782 783
outputting any debugging information is concerned, in other words nothing,
takes 14 seconds, and in the other case the code takes one hundredth of a
second.  Looks fairly definitive.  Use a <code class="inline"><span class="i">$DEBUG</span></code>
 variable BEFORE you call the
subroutine, rather than relying on the smart functionality inside it.</p>
<a name="Logging-if-DEBUG-(constant)"></a><h2>Logging if DEBUG (constant)</h2>
784
<p>It's possible to take the previous idea a little further, by using a compile
785 786 787
time <code class="inline"><span class="w">DEBUG</span></code>
 constant.</p>
<p># ifdebug-constant</p>
788 789
<pre class="verbatim"><ol><li> <span class="c">#!/usr/bin/perl</span></li><li></li><li> <a class="l_k" href="functions/use.html">use</a> <span class="w">strict</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/use.html">use</a> <span class="w">warnings</span><span class="sc">;</span></li><li></li><li> <a class="l_k" href="functions/use.html">use</a> <span class="w">Benchmark</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/use.html">use</a> <span class="w">Data::Dumper</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/use.html">use</a> <span class="w">constant</span></li><li>     <span class="i">DEBUG</span> <span class="cm">=&gt;</span> <span class="n">0</span></li><li> <span class="sc">;</span></li><li></li><li><a name="debug"></a> sub <span class="m">debug</span> <span class="s">{</span></li><li>     <a class="l_k" href="functions/if.html">if</a> <span class="s">(</span> <span class="i">DEBUG</span> <span class="s">)</span> <span class="s">{</span></li><li>         <a class="l_k" href="functions/my.html">my</a> <span class="i">$msg</span> = <a class="l_k" href="functions/shift.html">shift</a><span class="sc">;</span></li><li>         <a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;DEBUG: $msg\n&quot;</span><span class="sc">;</span></li><li>     <span class="s">}</span></li><li> <span class="s">}</span><span class="sc">;</span></li><li></li><li> <span class="i">timethese</span><span class="s">(</span><span class="n">100000</span><span class="cm">,</span> <span class="s">{</span></li><li>         <span class="q">&#39;debug&#39;</span>       <span class="cm">=&gt;</span> <a class="l_k" href="functions/sub.html">sub</a> <span class="s">{</span></li><li>             <span class="i">debug</span><span class="s">(</span> <span class="q">&quot;A $0 logging message via process-id: $$&quot;</span> . <span class="i">Dumper</span><span class="s">(</span>\<span class="i">%INC</span><span class="s">)</span> <span class="s">)</span></li><li>         <span class="s">}</span><span class="cm">,</span></li><li>         <span class="q">&#39;constant&#39;</span>  <span class="cm">=&gt;</span> <a class="l_k" href="functions/sub.html">sub</a> <span class="s">{</span></li><li>             <span class="i">debug</span><span class="s">(</span> <span class="q">&quot;A $0 logging message via process-id: $$&quot;</span> . <span class="i">Dumper</span><span class="s">(</span>\<span class="i">%INC</span><span class="s">)</span> <span class="s">)</span> <a class="l_k" href="functions/if.html">if</a> <span class="i">DEBUG</span></li><li>         <span class="s">}</span><span class="cm">,</span></li><li> <span class="s">}</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>Running this program produces the following output:</p>
<pre class="verbatim"><ol><li> <span class="i">$&gt;</span> <span class="w">perl</span> <span class="w">ifdebug</span>-<span class="w">constant</span></li><li> <span class="w">Benchmark</span><span class="co">:</span> <span class="w">timing</span> <span class="n">100000</span> <span class="w">iterations</span> <span class="w">of</span> <span class="w">constant</span><span class="cm">,</span> <a class="l_k" href="functions/sub.html">sub</a>...</li><li>   <span class="w">constant</span><span class="co">:</span>  <span class="n">0</span> <span class="w">wallclock</span> <span class="w">secs</span> <span class="s">(</span><span class="n">-0.00</span> <span class="w">usr</span> +  <span class="n">0.00</span> <span class="w">sys</span> = <span class="n">-0.00</span> <span class="w">CPU</span><span class="s">)</span> <span class="i">@ -</span><span class="n">7205759403792793600000.00</span>/<span class="q">s (n=100000)</span></li><li>             <span class="q">             (warning: too few iterations for a reliable count)</span></li><li>        <span class="i">sub</span><span class="co">:</span> <span class="n">14</span> <span class="w">wallclock</span> <span class="w">secs</span> <span class="s">(</span><span class="n">13.09</span> <span class="w">usr</span> +  <span class="n">0.00</span> <span class="w">sys</span> = <span class="n">13.09</span> <span class="w">CPU</span><span class="s">)</span> <span class="i">@ 7639</span>.<span class="n">42</span>/<span class="q">s (n=100000)</span></li></ol></pre><p>The <code class="inline"><span class="w">DEBUG</span></code>
790 791
 constant wipes the floor with even the <code class="inline"><span class="i">$debug</span></code>
 variable,
792 793
clocking in at minus zero seconds, and generates a "warning: too few iterations
for a reliable count" message into the bargain.  To see what is really going
794 795 796
on, and why we had too few iterations when we thought we asked for 100000, we
can use the very useful <code class="inline"><span class="w">B::Deparse</span></code>
 to inspect the new code:</p>
797
<pre class="verbatim"><ol><li> <span class="i">$&gt;</span> <span class="w">perl</span> -<span class="w">MO</span>=<span class="w">Deparse</span> <span class="w">ifdebug</span>-<span class="w">constant</span></li><li></li><li> <a class="l_k" href="functions/use.html">use</a> <span class="w">Benchmark</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/use.html">use</a> <span class="w">Data::Dumper</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/use.html">use</a> <span class="w">constant</span> <span class="s">(</span><span class="q">&#39;DEBUG&#39;</span><span class="cm">,</span> <span class="n">0</span><span class="s">)</span><span class="sc">;</span></li><li><a name="debug"></a> sub <span class="m">debug</span> <span class="s">{</span></li><li>     <a class="l_k" href="functions/use.html">use</a> <span class="w">warnings</span><span class="sc">;</span></li><li>     <a class="l_k" href="functions/use.html">use</a> <span class="w">strict</span> <span class="q">&#39;refs&#39;</span><span class="sc">;</span></li><li>     <span class="n">0</span><span class="sc">;</span></li><li> <span class="s">}</span></li><li> <a class="l_k" href="functions/use.html">use</a> <span class="w">warnings</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/use.html">use</a> <span class="w">strict</span> <span class="q">&#39;refs&#39;</span><span class="sc">;</span></li><li> <span class="i">timethese</span><span class="s">(</span><span class="n">100000</span><span class="cm">,</span> <span class="s">{</span><span class="q">&#39;sub&#39;</span><span class="cm">,</span> <a class="l_k" href="functions/sub.html">sub</a> <span class="s">{</span></li><li>     <span class="i">debug</span> <span class="q">&quot;A $0 logging message via process-id: $$&quot;</span> . <span class="i">Dumper</span><span class="s">(</span>\<span class="i">%INC</span><span class="s">)</span><span class="sc">;</span></li><li> <span class="s">}</span></li><li> <span class="cm">,</span> <span class="q">&#39;constant&#39;</span><span class="cm">,</span> <a class="l_k" href="functions/sub.html">sub</a> <span class="s">{</span></li><li>     <span class="n">0</span><span class="sc">;</span></li><li> <span class="s">}</span></li><li> <span class="s">}</span><span class="s">)</span><span class="sc">;</span></li><li> <span class="w">ifdebug</span>-<span class="w">constant</span> <span class="w">syntax</span> <span class="w">OK</span></li></ol></pre><p>The output shows the constant() subroutine we're testing being replaced with
798 799
the value of the <code class="inline"><span class="w">DEBUG</span></code>
 constant: zero.  The line to be tested has been
800
completely optimized away, and you can't get much more efficient than that.</p>
801 802 803
<a name="POSTSCRIPT"></a><h1>POSTSCRIPT</h1>
<p>This document has provided several way to go about identifying hot-spots, and
checking whether any modifications have improved the runtime of the code.</p>
804
<p>As a final thought, remember that it's not (at the time of writing) possible to
805 806 807
produce a useful program which will run in zero or negative time and this basic
principle can be written as: <i>useful programs are slow</i> by their very
definition.  It is of course possible to write a nearly instantaneous program,
808
but it's not going to do very much, here's a very efficient one:</p>
809
<pre class="verbatim"><ol><li> <span class="i">$&gt;</span> <span class="w">perl</span> -e <span class="n">0</span></li></ol></pre><p>Optimizing that any further is a job for <code class="inline"><span class="w">p5p</span></code>
810 811 812 813 814 815 816 817 818 819 820 821
.</p>
<a name="SEE-ALSO"></a><h1>SEE ALSO</h1>
<p>Further reading can be found using the modules and links below.</p>
<a name="PERLDOCS"></a><h2>PERLDOCS</h2>
<p>For example: <code class="inline"><span class="w">perldoc</span> -f <a class="l_k" href="functions/sort.html">sort</a></code>
.</p>
<p><a href="perlfaq4.html">perlfaq4</a>.</p>
<p><a href="perlfork.html">perlfork</a>, <a href="perlfunc.html">perlfunc</a>, <a href="perlretut.html">perlretut</a>, <a href="perlthrtut.html">perlthrtut</a>.</p>
<p><a href="threads.html">threads</a>.</p>
<a name="MAN-PAGES"></a><h2>MAN PAGES</h2>
<p><code class="inline"><a class="l_k" href="functions/time.html">time</a></code>.</p>
<a name="MODULES"></a><h2>MODULES</h2>
822 823
<p>It's not possible to individually showcase all the performance related code for
Perl here, naturally, but here's a short list of modules from the CPAN which
824
deserve further attention.</p>
825
<pre class="verbatim"><ol><li> <span class="w">Apache::DProf</span></li><li> <span class="w">Apache::SmallProf</span></li><li> <span class="w">Benchmark</span></li><li> <span class="w">DBIx::Profile</span></li><li> <span class="w">Devel::AutoProfiler</span></li><li> <span class="w">Devel::DProf</span></li><li> <span class="w">Devel::DProfLB</span></li><li> <span class="w">Devel::FastProf</span></li><li> <span class="w">Devel::GraphVizProf</span></li><li> <span class="w">Devel::NYTProf</span></li><li> <span class="w">Devel::NYTProf::Apache</span></li><li> <span class="w">Devel::Profiler</span></li><li> <span class="w">Devel::Profile</span></li><li> <span class="w">Devel::Profit</span></li><li> <span class="w">Devel::SmallProf</span></li><li> <span class="w">Devel::WxProf</span></li><li> <span class="w">POE::Devel::Profiler</span></li><li> <span class="w">Sort::Key</span></li><li> <span class="w">Sort::Maker</span></li></ol></pre><a name="URLS"></a><h2>URLS</h2>
826
<p>Very useful online reference material:</p>
827
<pre class="verbatim"><ol><li> <span class="j">http:</span><span class="q">//</span><span class="w">www</span>.<span class="w">ccl4</span>.<span class="w">org</span><span class="q">/~nick/</span><span class="w">P</span>/<span class="w">Fast_Enough</span>/</li><li></li><li> <span class="w">http</span><span class="co">:</span><span class="q">//</span><span class="w">www</span>-<span class="n">128.</span><span class="w">ibm</span>.<span class="w">com</span>/<span class="w">developerworks</span>/<span class="w">library</span>/<span class="w">l</span>-<span class="w">optperl</span>.<span class="w">html</span></li><li></li><li> <span class="w">http</span><span class="co">:</span><span class="q">//perlbuzz</span>.<span class="w">com</span>/<span class="n">2007</span>/<span class="n">11</span>/<a class="l_k" href="functions/bind.html">bind</a>-<span class="w">output</span>-<span class="w">variables</span>-<span class="w">in</span>-<span class="w">dbi</span>-<a class="l_k" href="functions/for.html">for</a>-<span class="w">speed</span>-<a class="l_k" href="functions/and.html">and</a>-<span class="w">safety</span>.<span class="w">html</span></li><li></li><li> <span class="w">http</span><span class="co">:</span><span class="q">//</span><span class="w">en</span>.<span class="w">wikipedia</span>.<span class="w">org</span><span class="q">/wiki/</span><span class="w">Performance_analysis</span></li><li></li><li> <span class="w">http</span><span class="co">:</span><span class="q">//</span><span class="w">apache</span>.<span class="w">perl</span>.<span class="w">org</span>/<span class="w">docs</span>/<span class="n">1.0</span>/<span class="w">guide</span>/<span class="w">performance</span>.<span class="w">html</span></li><li></li><li> <span class="w">http</span><span class="co">:</span><span class="q">//perlgolf</span>.<span class="w">sourceforge</span>.<span class="w">net</span>/</li><li></li><li> <span class="w">http</span><span class="co">:</span><span class="q">//</span><span class="w">www</span>.<span class="w">sysarch</span>.<span class="w">com</span>/<span class="w">Perl</span>/<span class="w">sort_paper</span>.<span class="w">html</span></li></ol></pre><a name="AUTHOR"></a><h1>AUTHOR</h1>
828 829 830 831 832
<p>Richard Foley &lt;richard.foley@rfi.net&gt; Copyright (c) 2008</p>




833 834 835 836 837
  <div id="page_index" class="hud_container">
    <div id="page_index_header" class="hud_header">
      <div id="page_index_close" class="hud_close"><a href="#" onClick="pageIndex.hide();return false;"></a></div>
      <div id="page_index_title" class="hud_title"><span class="hud_span_top">Page index</span></div>
      <div id="page_index_topright" class="hud_topright"></div>
838
    </div>
839
    <div id="page_index_content" class="hud_content">
840 841
      <ul><li><a href="#NAME">NAME</a><li><a href="#DESCRIPTION">DESCRIPTION</a><li><a href="#OVERVIEW">OVERVIEW</a><ul><li><a href="#ONE-STEP-SIDEWAYS">ONE STEP SIDEWAYS</a><li><a href="#ONE-STEP-FORWARD">ONE STEP FORWARD</a><li><a href="#ANOTHER-STEP-SIDEWAYS">