Skip to content
Tutorials Center
My learning

Topics

Lesson 35 of 43 ยท Tools and Quality

Performance and Profiling

Make Perl code faster the right way: measure with Benchmark, profile with Devel::NYTProf, avoid string copies, tame regexes, pick hashes or arrays, and save memory.

Perl is quick enough for most jobs, and its regex engine and hashes are heavily tuned. When a script is too slow or uses too much memory, the cause is nearly always a poor algorithm or an accidental copy of a large value, rarely the language itself. The hard part is finding the real bottleneck, because programmers are famously bad at guessing where time goes.

This lesson follows the professional order: measure first, find the hot spot, change one thing, measure again. You will meet Benchmark for comparing small alternatives, Devel::NYTProf for profiling a whole program, and the most common fixes for string handling, regular expressions, data structures and memory. At the end there is a short overview of going beyond Perl with XS and Inline.

Perl is not installed on this site, so the Perl code below is read-only. The interactive examples are Python stand-ins that demonstrate the ideas with counts of operations rather than timings, so their output is the same on every machine. Do not quote timings from a single run of any benchmark; they vary between computers and runs.

Measure before you optimize

Donald Knuth's warning that premature optimization is the root of much evil applies in full. Before changing anything:

  1. Write a test or a fixed input that reproduces the slow behavior.
  2. Measure the time (and memory) of the whole program.
  3. Profile it to see which subroutines and lines take the time.
  4. Fix the biggest cost, then measure again. Keep the change only if it helped.

The tuning loop

Measure Time the real program on real input.
Profile Find the line that costs the most.
Change one thing Fix the biggest cost only.
Repeat Keep the change only if it measures faster.
1 / 2

Comparing alternatives with Benchmark

Benchmark is a core module. Its cmpthese function runs several code snippets for a set amount of time and prints a comparison table. A negative first argument means "run for at least that many CPU seconds":

PERL
use v5.36;
use Benchmark qw(cmpthese);

my @words  = map { "word$_" } 1 .. 2000;
my %lookup = map { $_ => 1 } @words;
my $needle = 'word1999';

cmpthese(-2, {
    list_scan   => sub { my $found = grep { $_ eq $needle } @words },
    hash_lookup => sub { my $found = exists $lookup{$needle} },
});

The output is a table of rates (runs per second) and, for each pair, how much faster one is than the other:

TEXT
                Rate list_scan hash_lookup
list_scan    13559/s        --        -100%
hash_lookup 14303026/s   105384%          --

The exact numbers depend on your machine, but the shape will not change: a hash lookup is thousands of times faster than scanning a 2,000-element list. Three cautions about micro-benchmarks:

  • Make the benchmark test what your program really does, with realistic data sizes. An advantage at ten items may vanish or reverse at ten thousand.
  • Beware of code that Perl can optimize away because the result is not used. Store the result somewhere.
  • A 20% win on a snippet that takes 1% of the total runtime changes nothing. Use a profiler to decide what deserves attention.

Profiling with Devel::NYTProf

Devel::NYTProf (from CPAN) is the standard profiler. It records how many times each line and subroutine ran and how long it took, then writes an HTML report you can click through:

Terminal
perl -d:NYTProf script.pl      # writes nytprof.out
nytprofhtml                    # builds nytprof/index.html

Open nytprof/index.html in a browser. The report lists the slowest subroutines, shows each source file with per-line times, and can draw call graphs. Look at two numbers for each line: the time it took and the count of how often it ran. A cheap line executed millions of times, or an expensive line executed once, are both candidates; the first usually points to a loop you can restructure.

Profile with representative input, and profile the version you actually deploy (same Perl, same modules). A profile of a tiny test file can mislead you completely.

Choose the right data structure

The biggest wins come from replacing a slow algorithm, not from clever syntax. The classic example is membership testing. Looking for an item in an array means checking elements one by one, so the cost grows with the size of the array. A hash finds a key in roughly constant time no matter how many keys it has.

PERL
# Slow: scans @seen for every new item (quadratic overall)
my @seen;
for my $item (@items) {
    push @seen, $item unless grep { $_ eq $item } @seen;
}

# Fast: one hash lookup per item
my %seen;
my @unique = grep { !$seen{$_}++ } @items;

Other tips in the same spirit:

  • Use a hash for lookups by name and an array for ordered, numbered data. See Hash Patterns.
  • Do not call sort inside a loop when sorting once outside is enough, and use the Schwartzian transform (from List Functions and Sorting) when the sort key is expensive to compute.
  • Use List::Util functions such as sum, max, first and uniq. They are written in C, so they beat equivalent Perl loops.
  • Read the input once, build an index in a hash, and then look things up, instead of re-scanning a file for every query.

Count the work instead of timing it, so the effect is the same for everyone:

Python Counting comparisons: list scan or hash lookup (Python stand-in)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Multiplying the input by four multiplies the list scan's work by about sixteen, while the hash work only grows by four. That is the meaning of "quadratic" versus "linear", and no amount of micro-tuning rescues a quadratic loop on big data.

Avoid needless copying

Perl copies a scalar when you assign it. For a ten-character string that is nothing, for a 50 MB string it matters. Watch for these:

  • Passing big values to subs. process($big_string) is cheap because @_ aliases the argument, but my ($text) = @_; makes a copy. For large strings or arrays, pass a reference (process(\$big_string), process(\@big_list)) and work through it. See References.
  • Returning big lists. return @big; copies the list to the caller. Returning a reference avoids it.
  • Building a string a piece at a time. $out .= $piece appends in place and is efficient. Collecting pieces in an array and calling join once is also fine. What hurts is rebuilding a whole new string on each step, as in $out = $out . $piece . "\n" in a hot loop.
  • Slurping a file just to read it line by line. while (my $line = <$fh>) keeps one line in memory; my @lines = <$fh> holds the whole file.
PERL
sub total_length ($aref) {          # pass a reference, no copy of the array
    my $total = 0;
    $total += length for @$aref;
    return $total;
}

my @rows = map { "row $_" } 1 .. 100_000;
say total_length(\@rows);

Here is the same difference shown with Python's sharing and copying, so you can see which operations duplicate data:

Python Sharing versus copying (Python stand-in)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Sharing is fast but means a change through one name is visible through the other; copying is safe but costs time and memory. Perl references give you that same choice.

Regular expression costs

Perl's regex engine is excellent, but a few habits keep it that way:

  • Anchor and be specific. A pattern starting with ^ can fail immediately at the wrong position; .* at the front forces the engine to try every split. Prefer [^,]* to .* when you know what must not match.
  • Precompile with qr//. A pattern containing a variable may be recompiled each time unless it is compiled once: my $re = qr/$pattern/; and then use $line =~ $re. Build the qr once, outside the loop.
  • Avoid catastrophic backtracking. Nested quantifiers such as (a+)+b on a long string of a characters can take exponential time. Use possessive quantifiers (a++) or atomic groups (?>...) where the pattern allows, as covered in Advanced Regex.
  • Use plain string functions when they are enough. index, substr, eq and lc beat a regex for a fixed string.
  • Do not capture what you do not need. Use (?:...) for grouping.
  • Match the cheap condition first. /foo/ && /bar.*baz/ stops early on lines with no foo.

Modern Perls are good at optimizing simple patterns (literal prefixes, anchors, character classes), so write clear patterns first and measure only when a profile points at a regex line. The old study function no longer does anything useful and can be left out.

Memory

Perl scalars, arrays and hashes carry overhead, so a million small hashes can use a lot of memory. Some practical points:

  • Stream instead of slurp. Read, process and discard line by line.
  • Store compactly. An array of numbers is smaller than a hash with numeric keys, and an array of arrays is smaller than an array of hashes (at the cost of readable field names; constants for the indexes help).
  • Free what you finish with. Lexical variables are released when they go out of scope, so keep big structures in narrow scopes. undef $big; frees the data at once.
  • Beware of cycles. Two structures that point at each other are never freed by reference counting. Break the cycle with Scalar::Util::weaken, as described in the references lesson.
  • Measure. Devel::Size (total_size($ref)) reports how big a structure is, and NYTProf and Devel::Peek help with deeper questions.

Going beyond Perl

If a profile proves that a small, numeric inner loop dominates and no better algorithm exists, you have options:

  • Use a CPAN module that already contains C code (many text, math and JSON modules do).
  • Write a snippet of C with Inline::C, which compiles it on first run and caches the result; it is convenient for experiments.
  • Write a full XS module, the traditional way to bind C to Perl. It is powerful but takes some learning.
  • Call an existing shared library with FFI::Platypus, which needs no C compiler.
  • Run work in parallel with fork, Parallel::ForkManager or MCE when the job divides naturally.

Try the simpler fixes first. Most "slow Perl" problems disappear with a hash, a reference or a streaming loop.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What should you do before optimizing a slow Perl program?

    Guessing is unreliable. Time the whole program, then profile with a tool such as Devel::NYTProf, and fix the biggest cost first.
  2. 2. Which command profiles a script with Devel::NYTProf?

    The -d:NYTProf switch loads the profiler and writes nytprof.out, which nytprofhtml turns into an HTML report.
  3. 3. Why does my ($text) = @_; matter for performance with a 50 MB string?

    Assigning a scalar copies its value. Passing and using a reference avoids duplicating large data.
  4. 4. How do you check membership of many items fastest?

    A hash lookup takes about the same time regardless of size, while scanning an array grows with its length.
  5. 5. What is a good way to avoid recompiling a pattern that contains a variable inside a loop?

    my $re = qr/$pattern/; compiles once. Inside the loop, match with $line =~ $re.

Summary

  • Measure first: time the whole program, profile it with Devel::NYTProf, change one thing, measure again.
  • Use Benchmark::cmpthese to compare small alternatives, but never trust a micro-benchmark on unrealistic data.
  • Fix the algorithm before the syntax: hashes for lookups, one sort, indexes built once.
  • Avoid copying large values; pass references and stream files line by line.
  • Anchor regexes, precompile with qr//, watch for backtracking and prefer plain string functions when they suffice.
  • Save memory with compact structures, narrow scopes and weak references for cycles.
  • As a last resort, move hot code into C with Inline::C, XS or FFI::Platypus.

Next, learn how to keep programs safe in Security and Safe Perl.