The results are even more dramatic if you do away with some of the dogma.

The overhead of the subroutine calls may be a constant; but it is sufficiently large that it obscures the real differences. Particularly the dramatic cost of using subst for such trivial operations:

#!/usr/bin/perl use warnings; use strict; our $TEST //= 0; use Benchmark qw{ cmpthese }; sub precat { my ($string, $prefix) = @_; $string = $prefix . $string; $string } sub Substr { my ($string, $prefix) = @_; substr $string, 0, 0, $prefix; $string } sub subst { my ($string, $prefix) = @_; $string =~ s/^/$prefix/; $string } sub pc(\$@){ ${$_[0]} = $_[1] . ${$_[0]}; } sub sr(\$@){ substr ${$_[0]}, 0, 0, $_[1]; } sub st(\$@){ ${$_[0]} =~ s[^][$_[1]]; } cmpthese -1, { precat1 => q( precat 'def', 'abc' ), substr1 => q( Substr 'def', 'abc' ), subst1 => q( subst 'def', 'abc' ), precat2 => q( my $r = 'def'; pc $r, 'abc'; ), substr2 => q( my $r = 'def'; sr $r, 'abc'; ), subst2 => q( my $r = 'def'; st $r, 'abc'; ), precat3 => q( my $r = 'def'; $r = 'abc' . $r; ), substr3 => q( my $r = 'def'; substr $r, 0, 0, 'abc'; ), subst3 => q( my $r = 'def'; $r =~ s[^][abc]; ), }; __END__ Rate subst2 subst1 subst3 precat1 precat2 substr1 precat3 + substr2 substr3 subst2 338979/s -- -40% -46% -61% -73% -74% -77% + -78% -91% subst1 563106/s 66% -- -10% -35% -55% -57% -61% + -63% -85% subst3 625570/s 85% 11% -- -28% -50% -52% -57% + -59% -84% precat1 863641/s 155% 53% 38% -- -31% -34% -40% + -44% -77% precat2 1242736/s 267% 121% 99% 44% -- -6% -14% + -19% -67% substr1 1315147/s 288% 134% 110% 52% 6% -- -9% + -14% -66% precat3 1442465/s 326% 156% 131% 67% 16% 10% -- + -6% -62% substr2 1537398/s 354% 173% 146% 78% 24% 17% 7% + -- -60% substr3 3816524/s 1026% 578% 510% 342% 207% 190% 165% + 148% --

With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority". I knew I was on the right track :)
In the absence of evidence, opinion is indistinguishable from prejudice.

In reply to Re^6: Reversed .= operator by BrowserUk
in thread Reversed .= operator by 1nickt

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.