Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Here is an easy way to get some idea of the performance impact of using <code>$&amp;</code>. First off, you need to create two benchmark scripts. They will have most of the code in common:</p> <pre><code>#!/usr/bin/perl use strict; use warnings; use autodie; use File::Spec::Functions qw( devnull ); open my $output, '&gt;', devnull; my $str = &lt;&lt;EO_LIPSUM; Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. EO_LIPSUM use Benchmark qw( timethese ); </code></pre> <p>For the first benchmark add</p> <pre><code>### benchmark with $MATCH timethese -1, { match_var =&gt; sub { $str =~ /commodo/; print $output $&amp;; $str =~ /^Lorem|ipsum/ and print $output 'yes'; } } </code></pre> <p>and for the second benchmark file, use</p> <pre><code>timethese -1, { capture =&gt; sub { $str =~ /(commodo)/; print $output $1; $str =~ /^Lorem|ipsum/ and print $output 'yes'; } } </code></pre> <p>Now, let's run these benchmarks (they <strong>must</strong> be in separate files):</p> <pre> Benchmark: running capture for at least 1 CPU seconds... capture: 1 wallclock secs ( 1.05 usr + 0.00 sys = 1.05 CPU) @ 301485.20/s (n=315655) </pre> <pre> Benchmark: running match_var for at least 1 CPU seconds... match_var: 1 wallclock secs ( 1.22 usr + 0.02 sys = 1.23 CPU) @ 255591.09/s (n=315655) </pre> <p>That is, using <code>$&amp;</code> caused a slow down of about 15% in this case. The slowdown is due to the impact of <code>$&amp;</code> on the simple regular expression match. Without the </p> <pre><code>$str =~ /^Lorem|ipsum/ and print $output 'yes'; </code></pre> <p>line, the version with <code>$&amp;</code> actually performs faster.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload