{
    "content": [
        {
            "type": "text",
            "text": "# PERLPERF (man)\n\n## NAME\n\nperlperf - Perl Performance and Optimization Techniques\n\n## DESCRIPTION\n\nThis is an introduction to the use of performance and optimization techniques which can be\nused with particular reference to perl programs.  While many perl developers have come from\nother languages, and can use their prior knowledge where appropriate, there are many other\npeople who might benefit from a few perl specific pointers.  If you want the condensed\nversion, perhaps the best advice comes from the renowned Japanese Samurai, Miyamoto Musashi,\nwho said:\n\n## Sections\n\n- **NAME**\n- **DESCRIPTION**\n- **OVERVIEW**\n- **GENERAL GUIDELINES**\n- **BENCHMARKS** (2 subsections)\n- **PROFILING TOOLS** (5 subsections)\n- **SORTING**\n- **LOGGING** (1 subsections)\n- **POSTSCRIPT**\n- **SEE ALSO**\n- **AUTHOR**\n\nUse structuredContent.sections for detailed options, examples, and full documentation.\n"
        }
    ],
    "structuredContent": {
        "command": "PERLPERF",
        "section": "",
        "mode": "man",
        "summary": "perlperf - Perl Performance and Optimization Techniques",
        "synopsis": null,
        "tldr_summary": null,
        "tldr_examples": [],
        "tldr_source": null,
        "flags": [],
        "examples": [],
        "see_also": [],
        "section_outline": [
            {
                "name": "NAME",
                "lines": 2,
                "subsections": []
            },
            {
                "name": "DESCRIPTION",
                "lines": 11,
                "subsections": []
            },
            {
                "name": "OVERVIEW",
                "lines": 27,
                "subsections": []
            },
            {
                "name": "GENERAL GUIDELINES",
                "lines": 42,
                "subsections": []
            },
            {
                "name": "BENCHMARKS",
                "lines": 2,
                "subsections": [
                    {
                        "name": "Assigning and Dereferencing Variables.",
                        "lines": 61
                    },
                    {
                        "name": "Search and replace or tr",
                        "lines": 43
                    }
                ]
            },
            {
                "name": "PROFILING TOOLS",
                "lines": 126,
                "subsections": [
                    {
                        "name": "Devel::DProf",
                        "lines": 56
                    },
                    {
                        "name": "Devel::Profiler",
                        "lines": 60
                    },
                    {
                        "name": "Devel::SmallProf",
                        "lines": 56
                    },
                    {
                        "name": "Devel::FastProf",
                        "lines": 43
                    },
                    {
                        "name": "Devel::NYTProf",
                        "lines": 166
                    }
                ]
            },
            {
                "name": "SORTING",
                "lines": 198,
                "subsections": []
            },
            {
                "name": "LOGGING",
                "lines": 65,
                "subsections": [
                    {
                        "name": "Logging if DEBUG (constant)",
                        "lines": 70
                    }
                ]
            },
            {
                "name": "POSTSCRIPT",
                "lines": 13,
                "subsections": []
            },
            {
                "name": "SEE ALSO",
                "lines": 55,
                "subsections": []
            },
            {
                "name": "AUTHOR",
                "lines": 5,
                "subsections": []
            }
        ],
        "sections": {
            "NAME": {
                "content": "perlperf - Perl Performance and Optimization Techniques\n",
                "subsections": []
            },
            "DESCRIPTION": {
                "content": "This is an introduction to the use of performance and optimization techniques which can be\nused with particular reference to perl programs.  While many perl developers have come from\nother languages, and can use their prior knowledge where appropriate, there are many other\npeople who might benefit from a few perl specific pointers.  If you want the condensed\nversion, perhaps the best advice comes from the renowned Japanese Samurai, Miyamoto Musashi,\nwho said:\n\n\"Do Not Engage in Useless Activity\"\n\nin 1645.\n",
                "subsections": []
            },
            "OVERVIEW": {
                "content": "Perhaps the most common mistake programmers make is to attempt to optimize their code before\na program actually does anything useful - this is a bad idea.  There's no point in having an\nextremely fast program that doesn't work.  The first job is to get a program to correctly do\nsomething useful, (not to mention ensuring the test suite is fully functional), and only then\nto consider optimizing it.  Having decided to optimize existing working code, there are\nseveral simple but essential steps to consider which are intrinsic to any optimization\nprocess.\n\nONE STEP SIDEWAYS\nFirstly, you need to establish a baseline time for the existing code, which timing needs to\nbe reliable and repeatable.  You'll probably want to use the \"Benchmark\" or \"Devel::NYTProf\"\nmodules, or something similar, for this step, or perhaps the Unix system \"time\" utility,\nwhichever is appropriate.  See the base of this document for a longer list of benchmarking\nand profiling modules, and recommended further reading.\n\nONE STEP FORWARD\nNext, having examined the program for hot spots, (places where the code seems to run slowly),\nchange the code with the intention of making it run faster.  Using version control software,\nlike \"subversion\", will ensure no changes are irreversible.  It's too easy to fiddle here and\nfiddle there - don't change too much at any one time or you might not discover which piece of\ncode really was the slow bit.\n\nANOTHER STEP SIDEWAYS\nIt's not enough to say: \"that will make it run faster\", you have to check it.  Rerun the code\nunder control of the benchmarking or profiling modules, from the first step above, and check\nthat the new code executed the same task in less time.  Save your work and repeat...\n",
                "subsections": []
            },
            "GENERAL GUIDELINES": {
                "content": "The critical thing when considering performance is to remember there is no such thing as a\n\"Golden Bullet\", which is why there are no rules, only guidelines.\n\nIt is clear that inline code is going to be faster than subroutine or method calls, because\nthere is less overhead, but this approach has the disadvantage of being less maintainable and\ncomes at the cost of greater memory usage - there is no such thing as a free lunch.  If you\nare searching for an element in a list, it can be more efficient to store the data in a hash\nstructure, and then simply look to see whether the key is defined, rather than to loop\nthrough the entire array using grep() for instance.  substr() may be (a lot) faster than\ngrep() but not as flexible, so you have another trade-off to access.  Your code may contain a\nline which takes 0.01 of a second to execute which if you call it 1,000 times, quite likely\nin a program parsing even medium sized files for instance, you already have a 10 second\ndelay, in just one single code location, and if you call that line 100,000 times, your entire\nprogram will slow down to an unbearable crawl.\n\nUsing a subroutine as part of your sort is a powerful way to get exactly what you want, but\nwill usually be slower than the built-in alphabetic \"cmp\" and numeric \"<=>\" sort operators.\nIt is possible to make multiple passes over your data, building indices to make the upcoming\nsort more efficient, and to use what is known as the \"OM\" (Orcish Maneuver) to cache the sort\nkeys in advance.  The cache lookup, while a good idea, can itself be a source of slowdown by\nenforcing a double pass over the data - once to setup the cache, and once to sort the data.\nUsing \"pack()\" to extract the required sort key into a consistent string can be an efficient\nway to build a single string to compare, instead of using multiple sort keys, which makes it\npossible to use the standard, written in \"c\" and fast, perl \"sort()\" function on the output,\nand is the basis of the \"GRT\" (Guttman Rossler Transform).  Some string combinations can slow\nthe \"GRT\" down, by just being too plain complex for its own good.\n\nFor applications using database backends, the standard \"DBIx\" namespace has tries to help\nwith keeping things nippy, not least because it tries to not query the database until the\nlatest possible moment, but always read the docs which come with your choice of libraries.\nAmong the many issues facing developers dealing with databases should remain aware of is to\nalways use \"SQL\" placeholders and to consider pre-fetching data sets when this might prove\nadvantageous.  Splitting up a large file by assigning multiple processes to parsing a single\nfile, using say \"POE\", \"threads\" or \"fork\" can also be a useful way of optimizing your usage\nof the available \"CPU\" resources, though this technique is fraught with concurrency issues\nand demands high attention to detail.\n\nEvery case has a specific application and one or more exceptions, and there is no replacement\nfor running a few tests and finding out which method works best for your particular\nenvironment, this is why writing optimal code is not an exact science, and why we love using\nPerl so much - TMTOWTDI.\n",
                "subsections": []
            },
            "BENCHMARKS": {
                "content": "Here are a few examples to demonstrate usage of Perl's benchmarking tools.\n",
                "subsections": [
                    {
                        "name": "Assigning and Dereferencing Variables.",
                        "content": "I'm sure most of us have seen code which looks like, (or worse than), this:\n\nif ( $obj->{ref}->{myscore} >= $obj->{ref}->{yourscore} ) {\n...\n\nThis sort of code can be a real eyesore to read, as well as being very sensitive to typos,\nand it's much clearer to dereference the variable explicitly.  We're side-stepping the issue\nof working with object-oriented programming techniques to encapsulate variable access via\nmethods, only accessible through an object.  Here we're just discussing the technical\nimplementation of choice, and whether this has an effect on performance.  We can see whether\nthis dereferencing operation, has any overhead by putting comparative code in a file and\nrunning a \"Benchmark\" test.\n\n# dereference\n\n#!/usr/bin/perl\n\nuse strict;\nuse warnings;\n\nuse Benchmark;\n\nmy $ref = {\n'ref'   => {\nmyscore    => '100 + 1',\nyourscore  => '102 - 1',\n},\n};\n\ntimethese(1000000, {\n'direct'       => sub {\nmy $x = $ref->{ref}->{myscore} . $ref->{ref}->{yourscore} ;\n},\n'dereference'  => sub {\nmy $ref  = $ref->{ref};\nmy $myscore = $ref->{myscore};\nmy $yourscore = $ref->{yourscore};\nmy $x = $myscore . $yourscore;\n},\n});\n\nIt's essential to run any timing measurements a sufficient number of times so the numbers\nsettle on a numerical average, otherwise each run will naturally fluctuate due to variations\nin the environment, to reduce the effect of contention for \"CPU\" resources and network\nbandwidth for instance.  Running the above code for one million iterations, we can take a\nlook at the report output by the \"Benchmark\" module, to see which approach is the most\neffective.\n\n$> perl dereference\n\nBenchmark: timing 1000000 iterations of dereference, direct...\ndereference:  2 wallclock secs ( 1.59 usr +  0.00 sys =  1.59 CPU) @ 628930.82/s (n=1000000)\ndirect:  1 wallclock secs ( 1.20 usr +  0.00 sys =  1.20 CPU) @ 833333.33/s (n=1000000)\n\nThe difference is clear to see and the dereferencing approach is slower.  While it managed to\nexecute an average of 628,930 times a second during our test, the direct approach managed to\nrun an additional 204,403 times, unfortunately.  Unfortunately, because there are many\nexamples of code written using the multiple layer direct variable access, and it's usually\nhorrible.  It is, however, minusculy faster.  The question remains whether the minute gain is\nactually worth the eyestrain, or the loss of maintainability.\n"
                    },
                    {
                        "name": "Search and replace or tr",
                        "content": "If we have a string which needs to be modified, while a regex will almost always be much more\nflexible, \"tr\", an oft underused tool, can still be a useful.  One scenario might be replace\nall vowels with another character.  The regex solution might look like this:\n\n$str =~ s/[aeiou]/x/g\n\nThe \"tr\" alternative might look like this:\n\n$str =~ tr/aeiou/xxxxx/\n\nWe can put that into a test file which we can run to check which approach is the fastest,\nusing a global $STR variable to assign to the \"my $str\" variable so as to avoid perl trying\nto optimize any of the work away by noticing it's assigned only the once.\n\n# regex-transliterate\n\n#!/usr/bin/perl\n\nuse strict;\nuse warnings;\n\nuse Benchmark;\n\nmy $STR = \"$$-this and that\";\n\ntimethese( 1000000, {\n'sr'  => sub { my $str = $STR; $str =~ s/[aeiou]/x/g; return $str; },\n'tr'  => sub { my $str = $STR; $str =~ tr/aeiou/xxxxx/; return $str; },\n});\n\nRunning the code gives us our results:\n\n$> perl regex-transliterate\n\nBenchmark: timing 1000000 iterations of sr, tr...\nsr:  2 wallclock secs ( 1.19 usr +  0.00 sys =  1.19 CPU) @ 840336.13/s (n=1000000)\ntr:  0 wallclock secs ( 0.49 usr +  0.00 sys =  0.49 CPU) @ 2040816.33/s (n=1000000)\n\nThe \"tr\" version is a clear winner.  One solution is flexible, the other is fast - and it's\nappropriately the programmer's choice which to use.\n\nCheck the \"Benchmark\" docs for further useful techniques.\n"
                    }
                ]
            },
            "PROFILING TOOLS": {
                "content": "A slightly larger piece of code will provide something on which a profiler can produce more\nextensive reporting statistics.  This example uses the simplistic \"wordmatch\" program which\nparses a given input file and spews out a short report on the contents.\n\n# wordmatch\n\n#!/usr/bin/perl\n\nuse strict;\nuse warnings;\n\n=head1 NAME\n\nfilewords - word analysis of input file\n\n=head1 SYNOPSIS\n\nfilewords -f inputfilename [-d]\n\n=head1 DESCRIPTION\n\nThis program parses the given filename, specified with C<-f>, and\ndisplays a simple analysis of the words found therein.  Use the C<-d>\nswitch to enable debugging messages.\n\n=cut\n\nuse FileHandle;\nuse Getopt::Long;\n\nmy $debug   =  0;\nmy $file    = '';\n\nmy $result = GetOptions (\n'debug'         => \\$debug,\n'file=s'        => \\$file,\n);\ndie(\"invalid args\") unless $result;\n\nunless ( -f $file ) {\ndie(\"Usage: $0 -f filename [-d]\");\n}\nmy $FH = FileHandle->new(\"< $file\")\nor die(\"unable to open file($file): $!\");\n\nmy $iLINES = 0;\nmy $iWORDS = 0;\nmy %count   = ();\n\nmy @lines = <$FH>;\nforeach my $line ( @lines ) {\n$iLINES++;\n$line =~ s/\\n//;\nmy @words = split(/ +/, $line);\nmy $iwords = scalar(@words);\n$iWORDS = $iWORDS + $iwords;\ndebug(\"line: $iLINES supplying $iwords words: @words\");\nmy $iword = 0;\nforeach my $word ( @words ) {\n$iword++;\n$count{$iLINES}{spec} += matches($iword, $word,\n'[^a-zA-Z0-9]');\n$count{$iLINES}{only} += matches($iword, $word,\n'^[^a-zA-Z0-9]+$');\n$count{$iLINES}{cons} += matches($iword, $word,\n'^[(?i:bcdfghjklmnpqrstvwxyz)]+$');\n$count{$iLINES}{vows} += matches($iword, $word,\n'^[(?i:aeiou)]+$');\n$count{$iLINES}{caps} += matches($iword, $word,\n'^[(A-Z)]+$');\n}\n}\n\nprint report( %count );\n\nsub matches {\nmy $iwd  = shift;\nmy $word  = shift;\nmy $regex = shift;\nmy $has = 0;\n\nif ( $word =~ /($regex)/ ) {\n$has++ if $1;\n}\n\ndebug( \"word: $iwd \"\n. ($has ? 'matches' : 'does not match')\n. \" chars: /$regex/\");\n\nreturn $has;\n}\n\nsub report {\nmy %report = @;\nmy %rep;\n\nforeach my $line ( keys %report ) {\nforeach my $key ( keys $report{$line}->%* ) {\n$rep{$key} += $report{$line}{$key};\n}\n}\n\nmy $report = qq|\n$0 report for $file:\nlines in file: $iLINES\nwords in file: $iWORDS\nwords with special (non-word) characters: $ispec\nwords with only special (non-word) characters: $ionly\nwords with only consonants: $icons\nwords with only capital letters: $icaps\nwords with only vowels: $ivows\n|;\n\nreturn $report;\n}\n\nsub debug {\nmy $message = shift;\n\nif ( $debug ) {\nprint STDERR \"DBG: $message\\n\";\n}\n}\n\nexit 0;\n",
                "subsections": [
                    {
                        "name": "Devel::DProf",
                        "content": "This venerable module has been the de-facto standard for Perl code profiling for more than a\ndecade, but has been replaced by a number of other modules which have brought us back to the\n21st century.  Although you're recommended to evaluate your tool from the several mentioned\nhere and from the CPAN list at the base of this document, (and currently Devel::NYTProf seems\nto be the weapon of choice - see below), we'll take a quick look at the output from\nDevel::DProf first, to set a baseline for Perl profiling tools.  Run the above program under\nthe control of \"Devel::DProf\" by using the \"-d\" switch on the command-line.\n\n$> perl -d:DProf wordmatch -f perl5db.pl\n\n<...multiple lines snipped...>\n\nwordmatch report for perl5db.pl:\nlines in file: 9428\nwords in file: 50243\nwords with special (non-word) characters: 20480\nwords with only special (non-word) characters: 7790\nwords with only consonants: 4801\nwords with only capital letters: 1316\nwords with only vowels: 1701\n\n\"Devel::DProf\" produces a special file, called tmon.out by default, and this file is read by\nthe \"dprofpp\" program, which is already installed as part of the \"Devel::DProf\" distribution.\nIf you call \"dprofpp\" with no options, it will read the tmon.out file in the current\ndirectory and produce a human readable statistics report of the run of your program.  Note\nthat this may take a little time.\n\n$> dprofpp\n\nTotal Elapsed Time = 2.951677 Seconds\nUser+System Time = 2.871677 Seconds\nExclusive Times\n%Time ExclSec CumulS #Calls sec/call Csec/c  Name\n102.   2.945  3.003 251215   0.0000 0.0000  main::matches\n2.40   0.069  0.069 260643   0.0000 0.0000  main::debug\n1.74   0.050  0.050      1   0.0500 0.0500  main::report\n1.04   0.030  0.049      4   0.0075 0.0123  main::BEGIN\n0.35   0.010  0.010      3   0.0033 0.0033  Exporter::asheavy\n0.35   0.010  0.010      7   0.0014 0.0014  IO::File::BEGIN\n0.00       - -0.000      1        -      -  Getopt::Long::FindOption\n0.00       - -0.000      1        -      -  Symbol::BEGIN\n0.00       - -0.000      1        -      -  Fcntl::BEGIN\n0.00       - -0.000      1        -      -  Fcntl::bootstrap\n0.00       - -0.000      1        -      -  warnings::BEGIN\n0.00       - -0.000      1        -      -  IO::bootstrap\n0.00       - -0.000      1        -      -  Getopt::Long::ConfigDefaults\n0.00       - -0.000      1        -      -  Getopt::Long::Configure\n0.00       - -0.000      1        -      -  Symbol::gensym\n\n\"dprofpp\" will produce some quite detailed reporting on the activity of the \"wordmatch\"\nprogram.  The wallclock, user and system, times are at the top of the analysis, and after\nthis are the main columns defining which define the report.  Check the \"dprofpp\" docs for\ndetails of the many options it supports.\n\nSee also \"Apache::DProf\" which hooks \"Devel::DProf\" into \"modperl\".\n"
                    },
                    {
                        "name": "Devel::Profiler",
                        "content": "Let's take a look at the same program using a different profiler: \"Devel::Profiler\", a drop-\nin Perl-only replacement for \"Devel::DProf\".  The usage is very slightly different in that\ninstead of using the special \"-d:\" flag, you pull \"Devel::Profiler\" in directly as a module\nusing \"-M\".\n\n$> perl -MDevel::Profiler wordmatch -f perl5db.pl\n\n<...multiple lines snipped...>\n\nwordmatch report for perl5db.pl:\nlines in file: 9428\nwords in file: 50243\nwords with special (non-word) characters: 20480\nwords with only special (non-word) characters: 7790\nwords with only consonants: 4801\nwords with only capital letters: 1316\nwords with only vowels: 1701\n\n\"Devel::Profiler\" generates a tmon.out file which is compatible with the \"dprofpp\" program,\nthus saving the construction of a dedicated statistics reader program.  \"dprofpp\" usage is\ntherefore identical to the above example.\n\n$> dprofpp\n\nTotal Elapsed Time =   20.984 Seconds\nUser+System Time =   19.981 Seconds\nExclusive Times\n%Time ExclSec CumulS #Calls sec/call Csec/c  Name\n49.0   9.792 14.509 251215   0.0000 0.0001  main::matches\n24.4   4.887  4.887 260643   0.0000 0.0000  main::debug\n0.25   0.049  0.049      1   0.0490 0.0490  main::report\n0.00   0.000  0.000      1   0.0000 0.0000  Getopt::Long::GetOptions\n0.00   0.000  0.000      2   0.0000 0.0000  Getopt::Long::ParseOptionSpec\n0.00   0.000  0.000      1   0.0000 0.0000  Getopt::Long::FindOption\n0.00   0.000  0.000      1   0.0000 0.0000  IO::File::new\n0.00   0.000  0.000      1   0.0000 0.0000  IO::Handle::new\n0.00   0.000  0.000      1   0.0000 0.0000  Symbol::gensym\n0.00   0.000  0.000      1   0.0000 0.0000  IO::File::open\n\nInterestingly we get slightly different results, which is mostly because the algorithm which\ngenerates the report is different, even though the output file format was allegedly\nidentical.  The elapsed, user and system times are clearly showing the time it took for\n\"Devel::Profiler\" to execute its own run, but the column listings feel more accurate somehow\nthan the ones we had earlier from \"Devel::DProf\".  The 102% figure has disappeared, for\nexample.  This is where we have to use the tools at our disposal, and recognise their pros\nand cons, before using them.  Interestingly, the numbers of calls for each subroutine are\nidentical in the two reports, it's the percentages which differ.  As the author of\n\"Devel::Proviler\" writes:\n\n...running HTML::Template's test suite under Devel::DProf shows\noutput() taking NO time but Devel::Profiler shows around 10% of the\ntime is in output().  I don't know which to trust but my gut tells me\nsomething is wrong with Devel::DProf.  HTML::Template::output() is a\nbig routine that's called for every test. Either way, something needs\nfixing.\n\nYMMV.\n\nSee also \"Devel::Apache::Profiler\" which hooks \"Devel::Profiler\" into \"modperl\".\n"
                    },
                    {
                        "name": "Devel::SmallProf",
                        "content": "The \"Devel::SmallProf\" profiler examines the runtime of your Perl program and produces a\nline-by-line listing to show how many times each line was called, and how long each line took\nto execute.  It is called by supplying the familiar \"-d\" flag to Perl at runtime.\n\n$> perl -d:SmallProf wordmatch -f perl5db.pl\n\n<...multiple lines snipped...>\n\nwordmatch report for perl5db.pl:\nlines in file: 9428\nwords in file: 50243\nwords with special (non-word) characters: 20480\nwords with only special (non-word) characters: 7790\nwords with only consonants: 4801\nwords with only capital letters: 1316\nwords with only vowels: 1701\n\n\"Devel::SmallProf\" writes it's output into a file called smallprof.out, by default.  The\nformat of the file looks like this:\n\n<num> <time> <ctime> <line>:<text>\n\nWhen the program has terminated, the output may be examined and sorted using any standard\ntext filtering utilities.  Something like the following may be sufficient:\n\n$> cat smallprof.out | grep \\d*: | sort -k3 | tac | head -n20\n\n251215   1.65674   7.68000    75: if ( $word =~ /($regex)/ ) {\n251215   0.03264   4.40000    79: debug(\"word: $iwd \".($has ? 'matches' :\n251215   0.02693   4.10000    81: return $has;\n260643   0.02841   4.07000   128: if ( $debug ) {\n260643   0.02601   4.04000   126: my $message = shift;\n251215   0.02641   3.91000    73: my $has = 0;\n251215   0.03311   3.71000    70: my $iwd  = shift;\n251215   0.02699   3.69000    72: my $regex = shift;\n251215   0.02766   3.68000    71: my $word  = shift;\n50243   0.59726   1.00000    59:  $count{$iLINES}{cons} =\n50243   0.48175   0.92000    61:  $count{$iLINES}{spec} =\n50243   0.00644   0.89000    56:  my $icons = matches($iword, $word,\n50243   0.48837   0.88000    63:  $count{$iLINES}{caps} =\n50243   0.00516   0.88000    58:  my $icaps = matches($iword, $word, '^[(A-\n50243   0.00631   0.81000    54:  my $ispec = matches($iword, $word, '[^a-\n50243   0.00496   0.80000    57:  my $ivows = matches($iword, $word,\n50243   0.00688   0.80000    53:  $iword++;\n50243   0.48469   0.79000    62:  $count{$iLINES}{only} =\n50243   0.48928   0.77000    60:  $count{$iLINES}{vows} =\n50243   0.00683   0.75000    55:  my $ionly = matches($iword, $word, '^[^a-\n\nYou can immediately see a slightly different focus to the subroutine profiling modules, and\nwe start to see exactly which line of code is taking the most time.  That regex line is\nlooking a bit suspicious, for example.  Remember that these tools are supposed to be used\ntogether, there is no single best way to profile your code, you need to use the best tools\nfor the job.\n\nSee also \"Apache::SmallProf\" which hooks \"Devel::SmallProf\" into \"modperl\".\n"
                    },
                    {
                        "name": "Devel::FastProf",
                        "content": "\"Devel::FastProf\" is another Perl line profiler.  This was written with a view to getting a\nfaster line profiler, than is possible with for example \"Devel::SmallProf\", because it's\nwritten in \"C\".  To use \"Devel::FastProf\", supply the \"-d\" argument to Perl:\n\n$> perl -d:FastProf wordmatch -f perl5db.pl\n\n<...multiple lines snipped...>\n\nwordmatch report for perl5db.pl:\nlines in file: 9428\nwords in file: 50243\nwords with special (non-word) characters: 20480\nwords with only special (non-word) characters: 7790\nwords with only consonants: 4801\nwords with only capital letters: 1316\nwords with only vowels: 1701\n\n\"Devel::FastProf\" writes statistics to the file fastprof.out in the current directory.  The\noutput file, which can be specified, can be interpreted by using the \"fprofpp\" command-line\nprogram.\n\n$> fprofpp | head -n20\n\n# fprofpp output format is:\n# filename:line time count: source\nwordmatch:75 3.93338 251215: if ( $word =~ /($regex)/ ) {\nwordmatch:79 1.77774 251215: debug(\"word: $iwd \".($has ? 'matches' : 'does not match').\" chars: /$regex/\");\nwordmatch:81 1.47604 251215: return $has;\nwordmatch:126 1.43441 260643: my $message = shift;\nwordmatch:128 1.42156 260643: if ( $debug ) {\nwordmatch:70 1.36824 251215: my $iwd  = shift;\nwordmatch:71 1.36739 251215: my $word  = shift;\nwordmatch:72 1.35939 251215: my $regex = shift;\n\nStraightaway we can see that the number of times each line has been called is identical to\nthe \"Devel::SmallProf\" output, and the sequence is only very slightly different based on the\nordering of the amount of time each line took to execute, \"if ( $debug ) { \" and \"my $message\n= shift;\", for example.  The differences in the actual times recorded might be in the\nalgorithm used internally, or it could be due to system resource limitations or contention.\n\nSee also the DBIx::Profile which will profile database queries running under the \"DBIx::*\"\nnamespace.\n"
                    },
                    {
                        "name": "Devel::NYTProf",
                        "content": "\"Devel::NYTProf\" is the next generation of Perl code profiler, fixing many shortcomings in\nother tools and implementing many cool features.  First of all it can be used as either a\nline profiler, a block or a subroutine profiler, all at once.  It can also use sub-\nmicrosecond (100ns) resolution on systems which provide \"clockgettime()\".  It can be started\nand stopped even by the program being profiled.  It's a one-line entry to profile \"modperl\"\napplications.  It's written in \"c\" and is probably the fastest profiler available for Perl.\nThe list of coolness just goes on.  Enough of that, let's see how to it works - just use the\nfamiliar \"-d\" switch to plug it in and run the code.\n\n$> perl -d:NYTProf wordmatch -f perl5db.pl\n\nwordmatch report for perl5db.pl:\nlines in file: 9427\nwords in file: 50243\nwords with special (non-word) characters: 20480\nwords with only special (non-word) characters: 7790\nwords with only consonants: 4801\nwords with only capital letters: 1316\nwords with only vowels: 1701\n\n\"NYTProf\" will generate a report database into the file nytprof.out by default.  Human\nreadable reports can be generated from here by using the supplied \"nytprofhtml\" (HTML output)\nand \"nytprofcsv\" (CSV output) programs.  We've used the Unix system \"html2text\" utility to\nconvert the nytprof/index.html file for convenience here.\n\n$> html2text nytprof/index.html\n\nPerformance Profile Index\nFor wordmatch\nRun on Fri Sep 26 13:46:39 2008\nReported on Fri Sep 26 13:47:23 2008\n\nTop 15 Subroutines -- ordered by exclusive time\n|Calls |P |F |Inclusive|Exclusive|Subroutine                          |\n|      |  |  |Time     |Time     |                                    |\n|251215|5 |1 |13.09263 |10.47692 |main::              |matches        |\n|260642|2 |1 |2.71199  |2.71199  |main::              |debug          |\n|1     |1 |1 |0.21404  |0.21404  |main::              |report         |\n|2     |2 |2 |0.00511  |0.00511  |XSLoader::          |load (xsub)    |\n|14    |14|7 |0.00304  |0.00298  |Exporter::          |import         |\n|3     |1 |1 |0.00265  |0.00254  |Exporter::          |asheavy       |\n|10    |10|4 |0.00140  |0.00140  |vars::              |import         |\n|13    |13|1 |0.00129  |0.00109  |constant::          |import         |\n|1     |1 |1 |0.00360  |0.00096  |FileHandle::        |import         |\n|3     |3 |3 |0.00086  |0.00074  |warnings::register::|import         |\n|9     |3 |1 |0.00036  |0.00036  |strict::            |bits           |\n|13    |13|13|0.00032  |0.00029  |strict::            |import         |\n|2     |2 |2 |0.00020  |0.00020  |warnings::          |import         |\n|2     |1 |1 |0.00020  |0.00020  |Getopt::Long::      |ParseOptionSpec|\n|7     |7 |6 |0.00043  |0.00020  |strict::            |unimport       |\n\nFor more information see the full list of 189 subroutines.\n\nThe first part of the report already shows the critical information regarding which\nsubroutines are using the most time.  The next gives some statistics about the source files\nprofiled.\n\nSource Code Files -- ordered by exclusive time then name\n|Stmts  |Exclusive|Avg.   |Reports                     |Source File         |\n|       |Time     |       |                            |                    |\n|2699761|15.66654 |6e-06  |line   .    block   .    sub|wordmatch           |\n|35     |0.02187  |0.00062|line   .    block   .    sub|IO/Handle.pm        |\n|274    |0.01525  |0.00006|line   .    block   .    sub|Getopt/Long.pm      |\n|20     |0.00585  |0.00029|line   .    block   .    sub|Fcntl.pm            |\n|128    |0.00340  |0.00003|line   .    block   .    sub|Exporter/Heavy.pm   |\n|42     |0.00332  |0.00008|line   .    block   .    sub|IO/File.pm          |\n|261    |0.00308  |0.00001|line   .    block   .    sub|Exporter.pm         |\n|323    |0.00248  |8e-06  |line   .    block   .    sub|constant.pm         |\n|12     |0.00246  |0.00021|line   .    block   .    sub|File/Spec/Unix.pm   |\n|191    |0.00240  |0.00001|line   .    block   .    sub|vars.pm             |\n|77     |0.00201  |0.00003|line   .    block   .    sub|FileHandle.pm       |\n|12     |0.00198  |0.00016|line   .    block   .    sub|Carp.pm             |\n|14     |0.00175  |0.00013|line   .    block   .    sub|Symbol.pm           |\n|15     |0.00130  |0.00009|line   .    block   .    sub|IO.pm               |\n|22     |0.00120  |0.00005|line   .    block   .    sub|IO/Seekable.pm      |\n|198    |0.00085  |4e-06  |line   .    block   .    sub|warnings/register.pm|\n|114    |0.00080  |7e-06  |line   .    block   .    sub|strict.pm           |\n|47     |0.00068  |0.00001|line   .    block   .    sub|warnings.pm         |\n|27     |0.00054  |0.00002|line   .    block   .    sub|overload.pm         |\n|9      |0.00047  |0.00005|line   .    block   .    sub|SelectSaver.pm      |\n|13     |0.00045  |0.00003|line   .    block   .    sub|File/Spec.pm        |\n|2701595|15.73869 |       |Total                       |\n|128647 |0.74946  |       |Average                     |\n|       |0.00201  |0.00003|Median                      |\n|       |0.00121  |0.00003|Deviation                   |\n\nReport produced by the NYTProf 2.03 Perl profiler, developed by Tim Bunce and\nAdam Kaplan.\n\nAt this point, if you're using the html report, you can click through the various links to\nbore down into each subroutine and each line of code.  Because we're using the text reporting\nhere, and there's a whole directory full of reports built for each source file, we'll just\ndisplay a part of the corresponding wordmatch-line.html file, sufficient to give an idea of\nthe sort of output you can expect from this cool tool.\n\n$> html2text nytprof/wordmatch-line.html\n\nPerformance Profile -- -block view-.-line view-.-sub view-\nFor wordmatch\nRun on Fri Sep 26 13:46:39 2008\nReported on Fri Sep 26 13:47:22 2008\n\nFile wordmatch\n\nSubroutines -- ordered by exclusive time\n|Calls |P|F|Inclusive|Exclusive|Subroutine    |\n|      | | |Time     |Time     |              |\n|251215|5|1|13.09263 |10.47692 |main::|matches|\n|260642|2|1|2.71199  |2.71199  |main::|debug  |\n|1     |1|1|0.21404  |0.21404  |main::|report |\n|0     |0|0|0        |0        |main::|BEGIN  |\n\n\n|Line|Stmts.|Exclusive|Avg.   |Code                                           |\n|    |      |Time     |       |                                               |\n|1   |      |         |       |#!/usr/bin/perl                                |\n|2   |      |         |       |                                               |\n|    |      |         |       |use strict;                                    |\n|3   |3     |0.00086  |0.00029|# spent 0.00003s making 1 calls to strict::    |\n|    |      |         |       |import                                         |\n|    |      |         |       |use warnings;                                  |\n|4   |3     |0.01563  |0.00521|# spent 0.00012s making 1 calls to warnings::  |\n|    |      |         |       |import                                         |\n|5   |      |         |       |                                               |\n|6   |      |         |       |=head1 NAME                                    |\n|7   |      |         |       |                                               |\n|8   |      |         |       |filewords - word analysis of input file        |\n<...snip...>\n|62  |1     |0.00445  |0.00445|print report( %count );                        |\n|    |      |         |       |# spent 0.21404s making 1 calls to main::report|\n|63  |      |         |       |                                               |\n|    |      |         |       |# spent 23.56955s (10.47692+2.61571) within    |\n|    |      |         |       |main::matches which was called 251215 times,   |\n|    |      |         |       |avg 0.00005s/call: # 50243 times               |\n|    |      |         |       |(2.12134+0.51939s) at line 57 of wordmatch, avg|\n|    |      |         |       |0.00005s/call # 50243 times (2.17735+0.54550s) |\n|64  |      |         |       |at line 56 of wordmatch, avg 0.00005s/call #   |\n|    |      |         |       |50243 times (2.10992+0.51797s) at line 58 of   |\n|    |      |         |       |wordmatch, avg 0.00005s/call # 50243 times     |\n|    |      |         |       |(2.12696+0.51598s) at line 55 of wordmatch, avg|\n|    |      |         |       |0.00005s/call # 50243 times (1.94134+0.51687s) |\n|    |      |         |       |at line 54 of wordmatch, avg 0.00005s/call     |\n|    |      |         |       |sub matches {                                  |\n<...snip...>\n|102 |      |         |       |                                               |\n|    |      |         |       |# spent 2.71199s within main::debug which was  |\n|    |      |         |       |called 260642 times, avg 0.00001s/call: #      |\n|    |      |         |       |251215 times (2.61571+0s) by main::matches at  |\n|103 |      |         |       |line 74 of wordmatch, avg 0.00001s/call # 9427 |\n|    |      |         |       |times (0.09628+0s) at line 50 of wordmatch, avg|\n|    |      |         |       |0.00001s/call                                  |\n|    |      |         |       |sub debug {                                    |\n|104 |260642|0.58496  |2e-06  |my $message = shift;                           |\n|105 |      |         |       |                                               |\n|106 |260642|1.09917  |4e-06  |if ( $debug ) {                                |\n|107 |      |         |       |print STDERR \"DBG: $message\\n\";                |\n|108 |      |         |       |}                                              |\n|109 |      |         |       |}                                              |\n|110 |      |         |       |                                               |\n|111 |1     |0.01501  |0.01501|exit 0;                                        |\n|112 |      |         |       |                                               |\n\nOodles of very useful information in there - this seems to be the way forward.\n\nSee also \"Devel::NYTProf::Apache\" which hooks \"Devel::NYTProf\" into \"modperl\".\n"
                    }
                ]
            },
            "SORTING": {
                "content": "Perl modules are not the only tools a performance analyst has at their disposal, system tools\nlike \"time\" should not be overlooked as the next example shows, where we take a quick look at\nsorting.  Many books, theses and articles, have been written about efficient sorting\nalgorithms, and this is not the place to repeat such work, there's several good sorting\nmodules which deserve taking a look at too: \"Sort::Maker\", \"Sort::Key\" spring to mind.\nHowever, it's still possible to make some observations on certain Perl specific\ninterpretations on issues relating to sorting data sets and give an example or two with\nregard to how sorting large data volumes can effect performance.  Firstly, an often\noverlooked point when sorting large amounts of data, one can attempt to reduce the data set\nto be dealt with and in many cases \"grep()\" can be quite useful as a simple filter:\n\n@data = sort grep { /$filter/ } @incoming\n\nA command such as this can vastly reduce the volume of material to actually sort through in\nthe first place, and should not be too lightly disregarded purely on the basis of its\nsimplicity.  The \"KISS\" principle is too often overlooked - the next example uses the simple\nsystem \"time\" utility to demonstrate.  Let's take a look at an actual example of sorting the\ncontents of a large file, an apache logfile would do.  This one has over a quarter of a\nmillion lines, is 50M in size, and a snippet of it looks like this:\n\n# logfile\n\n188.209-65-87.adsl-dyn.isp.belgacom.be - - [08/Feb/2007:12:57:16 +0000] \"GET /favicon.ico HTTP/1.1\" 404 209 \"-\" \"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)\"\n188.209-65-87.adsl-dyn.isp.belgacom.be - - [08/Feb/2007:12:57:16 +0000] \"GET /favicon.ico HTTP/1.1\" 404 209 \"-\" \"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)\"\n151.56.71.198 - - [08/Feb/2007:12:57:41 +0000] \"GET /suse-on-vaio.html HTTP/1.1\" 200 2858 \"http://www.linux-on-laptops.com/sony.html\" \"Mozilla/5.0 (Windows; U; Windows NT 5.2; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1\"\n151.56.71.198 - - [08/Feb/2007:12:57:42 +0000] \"GET /data/css HTTP/1.1\" 404 206 \"http://www.rfi.net/suse-on-vaio.html\" \"Mozilla/5.0 (Windows; U; Windows NT 5.2; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1\"\n151.56.71.198 - - [08/Feb/2007:12:57:43 +0000] \"GET /favicon.ico HTTP/1.1\" 404 209 \"-\" \"Mozilla/5.0 (Windows; U; Windows NT 5.2; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1\"\n217.113.68.60 - - [08/Feb/2007:13:02:15 +0000] \"GET / HTTP/1.1\" 304 - \"-\" \"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)\"\n217.113.68.60 - - [08/Feb/2007:13:02:16 +0000] \"GET /data/css HTTP/1.1\" 404 206 \"http://www.rfi.net/\" \"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)\"\ndebora.to.isac.cnr.it - - [08/Feb/2007:13:03:58 +0000] \"GET /suse-on-vaio.html HTTP/1.1\" 200 2858 \"http://www.linux-on-laptops.com/sony.html\" \"Mozilla/5.0 (compatible; Konqueror/3.4; Linux) KHTML/3.4.0 (like Gecko)\"\ndebora.to.isac.cnr.it - - [08/Feb/2007:13:03:58 +0000] \"GET /data/css HTTP/1.1\" 404 206 \"http://www.rfi.net/suse-on-vaio.html\" \"Mozilla/5.0 (compatible; Konqueror/3.4; Linux) KHTML/3.4.0 (like Gecko)\"\ndebora.to.isac.cnr.it - - [08/Feb/2007:13:03:58 +0000] \"GET /favicon.ico HTTP/1.1\" 404 209 \"-\" \"Mozilla/5.0 (compatible; Konqueror/3.4; Linux) KHTML/3.4.0 (like Gecko)\"\n195.24.196.99 - - [08/Feb/2007:13:26:48 +0000] \"GET / HTTP/1.0\" 200 3309 \"-\" \"Mozilla/5.0 (Windows; U; Windows NT 5.1; fr; rv:1.8.0.9) Gecko/20061206 Firefox/1.5.0.9\"\n195.24.196.99 - - [08/Feb/2007:13:26:58 +0000] \"GET /data/css HTTP/1.0\" 404 206 \"http://www.rfi.net/\" \"Mozilla/5.0 (Windows; U; Windows NT 5.1; fr; rv:1.8.0.9) Gecko/20061206 Firefox/1.5.0.9\"\n195.24.196.99 - - [08/Feb/2007:13:26:59 +0000] \"GET /favicon.ico HTTP/1.0\" 404 209 \"-\" \"Mozilla/5.0 (Windows; U; Windows NT 5.1; fr; rv:1.8.0.9) Gecko/20061206 Firefox/1.5.0.9\"\ncrawl1.cosmixcorp.com - - [08/Feb/2007:13:27:57 +0000] \"GET /robots.txt HTTP/1.0\" 200 179 \"-\" \"voyager/1.0\"\ncrawl1.cosmixcorp.com - - [08/Feb/2007:13:28:25 +0000] \"GET /links.html HTTP/1.0\" 200 3413 \"-\" \"voyager/1.0\"\nfhm226.internetdsl.tpnet.pl - - [08/Feb/2007:13:37:32 +0000] \"GET /suse-on-vaio.html HTTP/1.1\" 200 2858 \"http://www.linux-on-laptops.com/sony.html\" \"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)\"\nfhm226.internetdsl.tpnet.pl - - [08/Feb/2007:13:37:34 +0000] \"GET /data/css HTTP/1.1\" 404 206 \"http://www.rfi.net/suse-on-vaio.html\" \"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)\"\n80.247.140.134 - - [08/Feb/2007:13:57:35 +0000] \"GET / HTTP/1.1\" 200 3309 \"-\" \"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322)\"\n80.247.140.134 - - [08/Feb/2007:13:57:37 +0000] \"GET /data/css HTTP/1.1\" 404 206 \"http://www.rfi.net\" \"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322)\"\npop.compuscan.co.za - - [08/Feb/2007:14:10:43 +0000] \"GET / HTTP/1.1\" 200 3309 \"-\" \"www.clamav.net\"\nlivebot-207-46-98-57.search.live.com - - [08/Feb/2007:14:12:04 +0000] \"GET /robots.txt HTTP/1.0\" 200 179 \"-\" \"msnbot/1.0 (+http://search.msn.com/msnbot.htm)\"\nlivebot-207-46-98-57.search.live.com - - [08/Feb/2007:14:12:04 +0000] \"GET /html/oracle.html HTTP/1.0\" 404 214 \"-\" \"msnbot/1.0 (+http://search.msn.com/msnbot.htm)\"\ndslb-088-064-005-154.pools.arcor-ip.net - - [08/Feb/2007:14:12:15 +0000] \"GET / HTTP/1.1\" 200 3309 \"-\" \"www.clamav.net\"\n196.201.92.41 - - [08/Feb/2007:14:15:01 +0000] \"GET / HTTP/1.1\" 200 3309 \"-\" \"MOT-L7/08.B7.DCR MIB/2.2.1 Profile/MIDP-2.0 Configuration/CLDC-1.1\"\n\nThe specific task here is to sort the 286,525 lines of this file by Response Code, Query,\nBrowser, Referring Url, and lastly Date.  One solution might be to use the following code,\nwhich iterates over the files given on the command-line.\n\n# sort-apache-log\n\n#!/usr/bin/perl -n\n\nuse strict;\nuse warnings;\n\nmy @data;\n\nLINE:\nwhile ( <> ) {\nmy $line = $;\nif (\n$line =~ m/^(\n([\\w\\.\\-]+)             # client\n\\s*-\\s*-\\s*\\[\n([^]]+)                 # date\n\\]\\s*\"\\w+\\s*\n(\\S+)                   # query\n[^\"]+\"\\s*\n(\\d+)                   # status\n\\s+\\S+\\s+\"[^\"]*\"\\s+\"\n([^\"]*)                 # browser\n\"\n.*\n)$/x\n) {\nmy @chunks = split(/ +/, $line);\nmy $ip      = $1;\nmy $date    = $2;\nmy $query   = $3;\nmy $status  = $4;\nmy $browser = $5;\n\npush(@data, [$ip, $date, $query, $status, $browser, $line]);\n}\n}\n\nmy @sorted = sort {\n$a->[3] cmp $b->[3]\n||\n$a->[2] cmp $b->[2]\n||\n$a->[0] cmp $b->[0]\n||\n$a->[1] cmp $b->[1]\n||\n$a->[4] cmp $b->[4]\n} @data;\n\nforeach my $data ( @sorted ) {\nprint $data->[5];\n}\n\nexit 0;\n\nWhen running this program, redirect \"STDOUT\" so it is possible to check the output is correct\nfrom following test runs and use the system \"time\" utility to check the overall runtime.\n\n$> time ./sort-apache-log logfile > out-sort\n\nreal    0m17.371s\nuser    0m15.757s\nsys     0m0.592s\n\nThe program took just over 17 wallclock seconds to run.  Note the different values \"time\"\noutputs, it's important to always use the same one, and to not confuse what each one means.\n\nElapsed Real Time\nThe overall, or wallclock, time between when \"time\" was called, and when it terminates.\nThe elapsed time includes both user and system times, and time spent waiting for other\nusers and processes on the system.  Inevitably, this is the most approximate of the\nmeasurements given.\n\nUser CPU Time\nThe user time is the amount of time the entire process spent on behalf of the user on\nthis system executing this program.\n\nSystem CPU Time\nThe system time is the amount of time the kernel itself spent executing routines, or\nsystem calls, on behalf of this process user.\n\nRunning this same process as a \"Schwarzian Transform\" it is possible to eliminate the input\nand output arrays for storing all the data, and work on the input directly as it arrives too.\nOtherwise, the code looks fairly similar:\n\n# sort-apache-log-schwarzian\n\n#!/usr/bin/perl -n\n\nuse strict;\nuse warnings;\n\nprint\n\nmap $->[0] =>\n\nsort {\n$a->[4] cmp $b->[4]\n||\n$a->[3] cmp $b->[3]\n||\n$a->[1] cmp $b->[1]\n||\n$a->[2] cmp $b->[2]\n||\n$a->[5] cmp $b->[5]\n}\nmap  [ $, m/^(\n([\\w\\.\\-]+)             # client\n\\s*-\\s*-\\s*\\[\n([^]]+)                 # date\n\\]\\s*\"\\w+\\s*\n(\\S+)                   # query\n[^\"]+\"\\s*\n(\\d+)                   # status\n\\s+\\S+\\s+\"[^\"]*\"\\s+\"\n([^\"]*)                 # browser\n\"\n.*\n)$/xo ]\n\n=> <>;\n\nexit 0;\n\nRun the new code against the same logfile, as above, to check the new time.\n\n$> time ./sort-apache-log-schwarzian logfile > out-schwarz\n\nreal    0m9.664s\nuser    0m8.873s\nsys     0m0.704s\n\nThe time has been cut in half, which is a respectable speed improvement by any standard.\nNaturally, it is important to check the output is consistent with the first program run, this\nis where the Unix system \"cksum\" utility comes in.\n\n$> cksum out-sort out-schwarz\n3044173777 52029194 out-sort\n3044173777 52029194 out-schwarz\n\nBTW. Beware too of pressure from managers who see you speed a program up by 50% of the\nruntime once, only to get a request one month later to do the same again (true story) -\nyou'll just have to point out you're only human, even if you are a Perl programmer, and\nyou'll see what you can do...\n",
                "subsections": []
            },
            "LOGGING": {
                "content": "An essential part of any good development process is appropriate error handling with\nappropriately informative messages, however there exists a school of thought which suggests\nthat log files should be chatty, as if the chain of unbroken output somehow ensures the\nsurvival of the program.  If speed is in any way an issue, this approach is wrong.\n\nA common sight is code which looks something like this:\n\nlogger->debug( \"A logging message via process-id: $$ INC: \"\n. Dumper(\\%INC) )\n\nThe problem is that this code will always be parsed and executed, even when the debug level\nset in the logging configuration file is zero.  Once the debug() subroutine has been entered,\nand the internal $debug variable confirmed to be zero, for example, the message which has\nbeen sent in will be discarded and the program will continue.  In the example given though,\nthe \"\\%INC\" hash will already have been dumped, and the message string constructed, all of\nwhich work could be bypassed by a debug variable at the statement level, like this:\n\nlogger->debug( \"A logging message via process-id: $$ INC: \"\n. Dumper(\\%INC) ) if $DEBUG;\n\nThis effect can be demonstrated by setting up a test script with both forms, including a\n\"debug()\" subroutine to emulate typical \"logger()\" functionality.\n\n# ifdebug\n\n#!/usr/bin/perl\n\nuse strict;\nuse warnings;\n\nuse Benchmark;\nuse Data::Dumper;\nmy $DEBUG = 0;\n\nsub debug {\nmy $msg = shift;\n\nif ( $DEBUG ) {\nprint \"DEBUG: $msg\\n\";\n}\n};\n\ntimethese(100000, {\n'debug'       => sub {\ndebug( \"A $0 logging message via process-id: $$\" . Dumper(\\%INC) )\n},\n'ifdebug'  => sub {\ndebug( \"A $0 logging message via process-id: $$\" . Dumper(\\%INC) ) if $DEBUG\n},\n});\n\nLet's see what \"Benchmark\" makes of this:\n\n$> perl ifdebug\nBenchmark: timing 100000 iterations of constant, sub...\nifdebug:  0 wallclock secs ( 0.01 usr +  0.00 sys =  0.01 CPU) @ 10000000.00/s (n=100000)\n(warning: too few iterations for a reliable count)\ndebug: 14 wallclock secs (13.18 usr +  0.04 sys = 13.22 CPU) @ 7564.30/s (n=100000)\n\nIn the one case the code, which does exactly the same thing as far as outputting any\ndebugging information is concerned, in other words nothing, takes 14 seconds, and in the\nother case the code takes one hundredth of a second.  Looks fairly definitive.  Use a $DEBUG\nvariable BEFORE you call the subroutine, rather than relying on the smart functionality\ninside it.\n",
                "subsections": [
                    {
                        "name": "Logging if DEBUG (constant)",
                        "content": "It's possible to take the previous idea a little further, by using a compile time \"DEBUG\"\nconstant.\n\n# ifdebug-constant\n\n#!/usr/bin/perl\n\nuse strict;\nuse warnings;\n\nuse Benchmark;\nuse Data::Dumper;\nuse constant\nDEBUG => 0\n;\n\nsub debug {\nif ( DEBUG ) {\nmy $msg = shift;\nprint \"DEBUG: $msg\\n\";\n}\n};\n\ntimethese(100000, {\n'debug'       => sub {\ndebug( \"A $0 logging message via process-id: $$\" . Dumper(\\%INC) )\n},\n'constant'  => sub {\ndebug( \"A $0 logging message via process-id: $$\" . Dumper(\\%INC) ) if DEBUG\n},\n});\n\nRunning this program produces the following output:\n\n$> perl ifdebug-constant\nBenchmark: timing 100000 iterations of constant, sub...\nconstant:  0 wallclock secs (-0.00 usr +  0.00 sys = -0.00 CPU) @ -7205759403792793600000.00/s (n=100000)\n(warning: too few iterations for a reliable count)\nsub: 14 wallclock secs (13.09 usr +  0.00 sys = 13.09 CPU) @ 7639.42/s (n=100000)\n\nThe \"DEBUG\" constant wipes the floor with even the $debug variable, clocking in at minus zero\nseconds, and generates a \"warning: too few iterations for a reliable count\" message into the\nbargain.  To see what is really going on, and why we had too few iterations when we thought\nwe asked for 100000, we can use the very useful \"B::Deparse\" to inspect the new code:\n\n$> perl -MO=Deparse ifdebug-constant\n\nuse Benchmark;\nuse Data::Dumper;\nuse constant ('DEBUG', 0);\nsub debug {\nuse warnings;\nuse strict 'refs';\n0;\n}\nuse warnings;\nuse strict 'refs';\ntimethese(100000, {'sub', sub {\ndebug \"A $0 logging message via process-id: $$\" . Dumper(\\%INC);\n}\n, 'constant', sub {\n0;\n}\n});\nifdebug-constant syntax OK\n\nThe output shows the constant() subroutine we're testing being replaced with the value of the\n\"DEBUG\" constant: zero.  The line to be tested has been completely optimized away, and you\ncan't get much more efficient than that.\n"
                    }
                ]
            },
            "POSTSCRIPT": {
                "content": "This document has provided several way to go about identifying hot-spots, and checking\nwhether any modifications have improved the runtime of the code.\n\nAs a final thought, remember that it's not (at the time of writing) possible to produce a\nuseful program which will run in zero or negative time and this basic principle can be\nwritten as: useful programs are slow by their very definition.  It is of course possible to\nwrite a nearly instantaneous program, but it's not going to do very much, here's a very\nefficient one:\n\n$> perl -e 0\n\nOptimizing that any further is a job for \"p5p\".\n",
                "subsections": []
            },
            "SEE ALSO": {
                "content": "Further reading can be found using the modules and links below.\n\nPERLDOCS\nFor example: \"perldoc -f sort\".\n\nperlfaq4.\n\nperlfork, perlfunc, perlretut, perlthrtut.\n\nthreads.\n\nMAN PAGES\n\"time\".\n\nMODULES\nIt's not possible to individually showcase all the performance related code for Perl here,\nnaturally, but here's a short list of modules from the CPAN which deserve further attention.\n\nApache::DProf\nApache::SmallProf\nBenchmark\nDBIx::Profile\nDevel::AutoProfiler\nDevel::DProf\nDevel::DProfLB\nDevel::FastProf\nDevel::GraphVizProf\nDevel::NYTProf\nDevel::NYTProf::Apache\nDevel::Profiler\nDevel::Profile\nDevel::Profit\nDevel::SmallProf\nDevel::WxProf\nPOE::Devel::Profiler\nSort::Key\nSort::Maker\n\nURLS\nVery useful online reference material:\n\nhttp://www.ccl4.org/~nick/P/FastEnough/\n\nhttp://www-128.ibm.com/developerworks/library/l-optperl.html\n\nhttp://perlbuzz.com/2007/11/bind-output-variables-in-dbi-for-speed-and-safety.html\n\nhttp://en.wikipedia.org/wiki/Performanceanalysis\n\nhttp://apache.perl.org/docs/1.0/guide/performance.html\n\nhttp://perlgolf.sourceforge.net/\n\nhttp://www.sysarch.com/Perl/sortpaper.html\n",
                "subsections": []
            },
            "AUTHOR": {
                "content": "Richard Foley <richard.foley@rfi.net> Copyright (c) 2008\n\n\n\nperl v5.34.0                                 2026-06-23                                  PERLPERF(1)",
                "subsections": []
            }
        }
    }
}