.\" -*- mode: troff; coding: utf-8 -*- .\" Automatically generated by Pod::Man 5.01 (Pod::Simple 3.43) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>. .ie n \{\ . ds C` "" . ds C' "" 'br\} .el\{\ . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" ======================================================================== .\" .IX Title "PERLPERF 1" .TH PERLPERF 1 2024-01-12 "perl v5.38.2" "Perl Programmers Reference Guide" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH NAME perlperf \- Perl Performance and Optimization Techniques .SH DESCRIPTION .IX Header "DESCRIPTION" 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: .PP .Vb 1 \& "Do Not Engage in Useless Activity" .Ve .PP in 1645. .SH OVERVIEW .IX Header "OVERVIEW" 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. There's no point in having an extremely fast program that doesn't work. The first job is to get a program to \fIcorrectly\fR do something \fBuseful\fR, (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. .SS "ONE STEP SIDEWAYS" .IX Subsection "ONE STEP SIDEWAYS" Firstly, you need to establish a baseline time for the existing code, which timing needs to be reliable and repeatable. You'll probably want to use the \&\f(CW\*(C`Benchmark\*(C'\fR or \f(CW\*(C`Devel::NYTProf\*(C'\fR modules, or something similar, for this step, or perhaps the Unix system \f(CW\*(C`time\*(C'\fR utility, whichever is appropriate. See the base of this document for a longer list of benchmarking and profiling modules, and recommended further reading. .SS "ONE STEP FORWARD" .IX Subsection "ONE STEP FORWARD" Next, having examined the program for \fIhot spots\fR, (places where the code seems to run slowly), change the code with the intention of making it run faster. Using version control software, like \f(CW\*(C`subversion\*(C'\fR, will ensure no 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 code \fBreally\fR was the slow bit. .SS "ANOTHER STEP SIDEWAYS" .IX Subsection "ANOTHER STEP SIDEWAYS" It's not enough to say: "that will make it run faster", you have to check it. Rerun the code under control of the benchmarking or profiling modules, from the first step above, and check that the new code executed the \fBsame task\fR in \&\fIless time\fR. Save your work and repeat... .SH "GENERAL GUIDELINES" .IX Header "GENERAL GUIDELINES" The critical thing when considering performance is to remember there is no such thing as a \f(CW\*(C`Golden Bullet\*(C'\fR, which is why there are no rules, only guidelines. .PP 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 \fBgrep()\fR for instance. \fBsubstr()\fR may be (a lot) faster than \fBgrep()\fR 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. .PP 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 \fIalphabetic\fR \f(CW\*(C`cmp\*(C'\fR and \&\fInumeric\fR \f(CW\*(C`<=>\*(C'\fR 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 \f(CW\*(C`OM\*(C'\fR (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 \f(CWpack()\fR 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 \f(CW\*(C`c\*(C'\fR and fast, perl \f(CWsort()\fR function on the output, and is the basis of the \f(CW\*(C`GRT\*(C'\fR (Guttman Rossler Transform). Some string combinations can slow the \f(CW\*(C`GRT\*(C'\fR down, by just being too plain complex for its own good. .PP For applications using database backends, the standard \f(CW\*(C`DBIx\*(C'\fR namespace has tries to help with keeping things nippy, not least because it tries to \fInot\fR 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 \&\f(CW\*(C`SQL\*(C'\fR 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 \f(CW\*(C`POE\*(C'\fR, \f(CW\*(C`threads\*(C'\fR or \f(CW\*(C`fork\*(C'\fR can also be a useful way of optimizing your usage of the available \f(CW\*(C`CPU\*(C'\fR resources, though this technique is fraught with concurrency issues and demands high attention to detail. .PP 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. .SH BENCHMARKS .IX Header "BENCHMARKS" Here are a few examples to demonstrate usage of Perl's benchmarking tools. .SS "Assigning and Dereferencing Variables." .IX Subsection "Assigning and Dereferencing Variables." I'm sure most of us have seen code which looks like, (or worse than), this: .PP .Vb 2 \& if ( $obj\->{_ref}\->{_myscore} >= $obj\->{_ref}\->{_yourscore} ) { \& ... .Ve .PP This sort of code can be a real eyesore to read, as well as being very sensitive to typos, and it's much clearer to dereference the variable explicitly. We're side-stepping the issue of working with object-oriented programming techniques to encapsulate variable access via methods, only accessible through an object. Here we're just discussing the technical 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 \f(CW\*(C`Benchmark\*(C'\fR test. .PP # dereference .PP .Vb 1 \& #!/usr/bin/perl \& \& use v5.36; \& \& use Benchmark; \& \& my $ref = { \& \*(Aqref\*(Aq => { \& _myscore => \*(Aq100 + 1\*(Aq, \& _yourscore => \*(Aq102 \- 1\*(Aq, \& }, \& }; \& \& timethese(1000000, { \& \*(Aqdirect\*(Aq => sub { \& my $x = $ref\->{ref}\->{_myscore} . $ref\->{ref}\->{_yourscore} ; \& }, \& \*(Aqdereference\*(Aq => sub { \& my $ref = $ref\->{ref}; \& my $myscore = $ref\->{_myscore}; \& my $yourscore = $ref\->{_yourscore}; \& my $x = $myscore . $yourscore; \& }, \& }); .Ve .PP It's essential to run any timing measurements a sufficient number of times so 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 \f(CW\*(C`CPU\*(C'\fR 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 \f(CW\*(C`Benchmark\*(C'\fR module, to see which approach is the most effective. .PP .Vb 1 \& $> perl dereference \& \& Benchmark: timing 1000000 iterations of dereference, direct... \& dereference: 2 wallclock secs ( 1.59 usr + 0.00 sys = 1.59 CPU) @ 628930.82/s (n=1000000) \& direct: 1 wallclock secs ( 1.20 usr + 0.00 sys = 1.20 CPU) @ 833333.33/s (n=1000000) .Ve .PP The difference is clear to see and the dereferencing approach is slower. While 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 multiple layer direct variable access, and it's usually horrible. It is, however, minusculy faster. The question remains whether the minute gain is actually worth the eyestrain, or the loss of maintainability. .SS "Search and replace or tr" .IX Subsection "Search and replace or tr" If we have a string which needs to be modified, while a regex will almost always be much more flexible, \f(CW\*(C`tr\*(C'\fR, 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: .PP .Vb 1 \& $str =~ s/[aeiou]/x/g .Ve .PP The \f(CW\*(C`tr\*(C'\fR alternative might look like this: .PP .Vb 1 \& $str =~ tr/aeiou/xxxxx/ .Ve .PP We can put that into a test file which we can run to check which approach is the fastest, using a global \f(CW$STR\fR variable to assign to the \f(CW\*(C`my $str\*(C'\fR variable so as to avoid perl trying to optimize any of the work away by noticing it's assigned only the once. .PP # regex-transliterate .PP .Vb 1 \& #!/usr/bin/perl \& \& use v5.36; \& \& use Benchmark; \& \& my $STR = "$$\-this and that"; \& \& timethese( 1000000, { \& \*(Aqsr\*(Aq => sub { my $str = $STR; $str =~ s/[aeiou]/x/g; return $str; }, \& \*(Aqtr\*(Aq => sub { my $str = $STR; $str =~ tr/aeiou/xxxxx/; return $str; }, \& }); .Ve .PP Running the code gives us our results: .PP .Vb 1 \& $> perl regex\-transliterate \& \& Benchmark: timing 1000000 iterations of sr, tr... \& sr: 2 wallclock secs ( 1.19 usr + 0.00 sys = 1.19 CPU) @ 840336.13/s (n=1000000) \& tr: 0 wallclock secs ( 0.49 usr + 0.00 sys = 0.49 CPU) @ 2040816.33/s (n=1000000) .Ve .PP The \f(CW\*(C`tr\*(C'\fR version is a clear winner. One solution is flexible, the other is fast \- and it's appropriately the programmer's choice which to use. .PP Check the \f(CW\*(C`Benchmark\*(C'\fR docs for further useful techniques. .SH "PROFILING TOOLS" .IX Header "PROFILING TOOLS" A slightly larger piece of code will provide something on which a profiler can produce more extensive reporting statistics. This example uses the simplistic \&\f(CW\*(C`wordmatch\*(C'\fR program which parses a given input file and spews out a short report on the contents. .PP # wordmatch .PP .Vb 1 \& #!/usr/bin/perl \& \& use v5.36; \& \& =head1 NAME \& \& filewords \- word analysis of input file \& \& =head1 SYNOPSIS \& \& filewords \-f inputfilename [\-d] \& \& =head1 DESCRIPTION \& \& This program parses the given filename, specified with C<\-f>, and \& displays a simple analysis of the words found therein. Use the C<\-d> \& switch to enable debugging messages. \& \& =cut \& \& use FileHandle; \& use Getopt::Long; \& \& my $debug = 0; \& my $file = \*(Aq\*(Aq; \& \& my $result = GetOptions ( \& \*(Aqdebug\*(Aq => \e$debug, \& \*(Aqfile=s\*(Aq => \e$file, \& ); \& die("invalid args") unless $result; \& \& unless ( \-f $file ) { \& die("Usage: $0 \-f filename [\-d]"); \& } \& my $FH = FileHandle\->new("< $file") \& or die("unable to open file($file): $!"); \& \& my $i_LINES = 0; \& my $i_WORDS = 0; \& my %count = (); \& \& my @lines = <$FH>; \& foreach my $line ( @lines ) { \& $i_LINES++; \& $line =~ s/\en//; \& my @words = split(/ +/, $line); \& my $i_words = scalar(@words); \& $i_WORDS = $i_WORDS + $i_words; \& debug("line: $i_LINES supplying $i_words words: @words"); \& my $i_word = 0; \& foreach my $word ( @words ) { \& $i_word++; \& $count{$i_LINES}{spec} += matches($i_word, $word, \& \*(Aq[^a\-zA\-Z0\-9]\*(Aq); \& $count{$i_LINES}{only} += matches($i_word, $word, \& \*(Aq^[^a\-zA\-Z0\-9]+$\*(Aq); \& $count{$i_LINES}{cons} += matches($i_word, $word, \& \*(Aq^[(?i:bcdfghjklmnpqrstvwxyz)]+$\*(Aq); \& $count{$i_LINES}{vows} += matches($i_word, $word, \& \*(Aq^[(?i:aeiou)]+$\*(Aq); \& $count{$i_LINES}{caps} += matches($i_word, $word, \& \*(Aq^[(A\-Z)]+$\*(Aq); \& } \& } \& \& print report( %count ); \& \& sub matches { \& my $i_wd = shift; \& my $word = shift; \& my $regex = shift; \& my $has = 0; \& \& if ( $word =~ /($regex)/ ) { \& $has++ if $1; \& } \& \& debug( "word: $i_wd " \& . ($has ? \*(Aqmatches\*(Aq : \*(Aqdoes not match\*(Aq) \& . " chars: /$regex/"); \& \& return $has; \& } \& \& sub report { \& my %report = @_; \& my %rep; \& \& foreach my $line ( keys %report ) { \& foreach my $key ( keys $report{$line}\->%* ) { \& $rep{$key} += $report{$line}{$key}; \& } \& } \& \& my $report = qq| \& $0 report for $file: \& lines in file: $i_LINES \& words in file: $i_WORDS \& words with special (non\-word) characters: $i_spec \& words with only special (non\-word) characters: $i_only \& words with only consonants: $i_cons \& words with only capital letters: $i_caps \& words with only vowels: $i_vows \& |; \& \& return $report; \& } \& \& sub debug { \& my $message = shift; \& \& if ( $debug ) { \& print STDERR "DBG: $message\en"; \& } \& } \& \& exit 0; .Ve .SS Devel::DProf .IX Subsection "Devel::DProf" 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 which have brought us back to the 21st century. Although you're recommended to evaluate your tool from the several mentioned here and from the CPAN list at the base of this document, (and currently Devel::NYTProf seems to be the weapon of choice \- see below), we'll take a quick look at the output from Devel::DProf first, to set a baseline for Perl profiling tools. Run the above program under the control of \f(CW\*(C`Devel::DProf\*(C'\fR by using the \f(CW\*(C`\-d\*(C'\fR switch on the command-line. .PP .Vb 1 \& $> perl \-d:DProf wordmatch \-f perl5db.pl \& \& <...multiple lines snipped...> \& \& wordmatch report for perl5db.pl: \& lines in file: 9428 \& words in file: 50243 \& words with special (non\-word) characters: 20480 \& words with only special (non\-word) characters: 7790 \& words with only consonants: 4801 \& words with only capital letters: 1316 \& words with only vowels: 1701 .Ve .PP \&\f(CW\*(C`Devel::DProf\*(C'\fR produces a special file, called \fItmon.out\fR by default, and this file is read by the \f(CW\*(C`dprofpp\*(C'\fR program, which is already installed as part of the \f(CW\*(C`Devel::DProf\*(C'\fR distribution. If you call \f(CW\*(C`dprofpp\*(C'\fR with no options, it will read the \fItmon.out\fR 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. .PP .Vb 1 \& $> dprofpp \& \& Total Elapsed Time = 2.951677 Seconds \& User+System Time = 2.871677 Seconds \& Exclusive Times \& %Time ExclSec CumulS #Calls sec/call Csec/c Name \& 102. 2.945 3.003 251215 0.0000 0.0000 main::matches \& 2.40 0.069 0.069 260643 0.0000 0.0000 main::debug \& 1.74 0.050 0.050 1 0.0500 0.0500 main::report \& 1.04 0.030 0.049 4 0.0075 0.0123 main::BEGIN \& 0.35 0.010 0.010 3 0.0033 0.0033 Exporter::as_heavy \& 0.35 0.010 0.010 7 0.0014 0.0014 IO::File::BEGIN \& 0.00 \- \-0.000 1 \- \- Getopt::Long::FindOption \& 0.00 \- \-0.000 1 \- \- Symbol::BEGIN \& 0.00 \- \-0.000 1 \- \- Fcntl::BEGIN \& 0.00 \- \-0.000 1 \- \- Fcntl::bootstrap \& 0.00 \- \-0.000 1 \- \- warnings::BEGIN \& 0.00 \- \-0.000 1 \- \- IO::bootstrap \& 0.00 \- \-0.000 1 \- \- Getopt::Long::ConfigDefaults \& 0.00 \- \-0.000 1 \- \- Getopt::Long::Configure \& 0.00 \- \-0.000 1 \- \- Symbol::gensym .Ve .PP \&\f(CW\*(C`dprofpp\*(C'\fR will produce some quite detailed reporting on the activity of the \&\f(CW\*(C`wordmatch\*(C'\fR 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 \f(CW\*(C`dprofpp\*(C'\fR docs for details of the many options it supports. .PP See also \f(CW\*(C`Apache::DProf\*(C'\fR which hooks \f(CW\*(C`Devel::DProf\*(C'\fR into \f(CW\*(C`mod_perl\*(C'\fR. .SS Devel::Profiler .IX Subsection "Devel::Profiler" Let's take a look at the same program using a different profiler: \&\f(CW\*(C`Devel::Profiler\*(C'\fR, a drop-in Perl-only replacement for \f(CW\*(C`Devel::DProf\*(C'\fR. The usage is very slightly different in that instead of using the special \f(CW\*(C`\-d:\*(C'\fR flag, you pull \f(CW\*(C`Devel::Profiler\*(C'\fR in directly as a module using \f(CW\*(C`\-M\*(C'\fR. .PP .Vb 1 \& $> perl \-MDevel::Profiler wordmatch \-f perl5db.pl \& \& <...multiple lines snipped...> \& \& wordmatch report for perl5db.pl: \& lines in file: 9428 \& words in file: 50243 \& words with special (non\-word) characters: 20480 \& words with only special (non\-word) characters: 7790 \& words with only consonants: 4801 \& words with only capital letters: 1316 \& words with only vowels: 1701 .Ve .PP \&\f(CW\*(C`Devel::Profiler\*(C'\fR generates a tmon.out file which is compatible with the \&\f(CW\*(C`dprofpp\*(C'\fR program, thus saving the construction of a dedicated statistics reader program. \f(CW\*(C`dprofpp\*(C'\fR usage is therefore identical to the above example. .PP .Vb 1 \& $> dprofpp \& \& Total Elapsed Time = 20.984 Seconds \& User+System Time = 19.981 Seconds \& Exclusive Times \& %Time ExclSec CumulS #Calls sec/call Csec/c Name \& 49.0 9.792 14.509 251215 0.0000 0.0001 main::matches \& 24.4 4.887 4.887 260643 0.0000 0.0000 main::debug \& 0.25 0.049 0.049 1 0.0490 0.0490 main::report \& 0.00 0.000 0.000 1 0.0000 0.0000 Getopt::Long::GetOptions \& 0.00 0.000 0.000 2 0.0000 0.0000 Getopt::Long::ParseOptionSpec \& 0.00 0.000 0.000 1 0.0000 0.0000 Getopt::Long::FindOption \& 0.00 0.000 0.000 1 0.0000 0.0000 IO::File::new \& 0.00 0.000 0.000 1 0.0000 0.0000 IO::Handle::new \& 0.00 0.000 0.000 1 0.0000 0.0000 Symbol::gensym \& 0.00 0.000 0.000 1 0.0000 0.0000 IO::File::open .Ve .PP Interestingly we get slightly different results, which is mostly because the 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 \f(CW\*(C`Devel::Profiler\*(C'\fR to execute its own run, but the column listings feel more accurate somehow than the ones we had earlier from \f(CW\*(C`Devel::DProf\*(C'\fR. 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 subroutine are identical in the two reports, it's the percentages which differ. As the author of \f(CW\*(C`Devel::Profiler\*(C'\fR writes: .PP .Vb 6 \& ...running HTML::Template\*(Aqs test suite under Devel::DProf shows \& output() taking NO time but Devel::Profiler shows around 10% of the \& time is in output(). I don\*(Aqt know which to trust but my gut tells me \& something is wrong with Devel::DProf. HTML::Template::output() is a \& big routine that\*(Aqs called for every test. Either way, something needs \& fixing. .Ve .PP YMMV. .PP See also \f(CW\*(C`Devel::Apache::Profiler\*(C'\fR which hooks \f(CW\*(C`Devel::Profiler\*(C'\fR into \f(CW\*(C`mod_perl\*(C'\fR. .SS Devel::SmallProf .IX Subsection "Devel::SmallProf" The \f(CW\*(C`Devel::SmallProf\*(C'\fR 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 \&\f(CW\*(C`\-d\*(C'\fR flag to Perl at runtime. .PP .Vb 1 \& $> perl \-d:SmallProf wordmatch \-f perl5db.pl \& \& <...multiple lines snipped...> \& \& wordmatch report for perl5db.pl: \& lines in file: 9428 \& words in file: 50243 \& words with special (non\-word) characters: 20480 \& words with only special (non\-word) characters: 7790 \& words with only consonants: 4801 \& words with only capital letters: 1316 \& words with only vowels: 1701 .Ve .PP \&\f(CW\*(C`Devel::SmallProf\*(C'\fR writes its output into a file called \fIsmallprof.out\fR, by default. The format of the file looks like this: .PP .Vb 1 \&