I don't get such a large difference, but then, I don't muddy the waters by using subs or by calling external programs.
use warnings 'all'; use strict; use Benchmark 'cmpthese'; $::unc = '\\\\server_name\\sys_share'; cmpthese -5 => { blackadder => 'my $tmp = $::unc; $tmp =~ s/^\W*\w+//; $server = $&; $server =~ s/^\W+//;', theorbtwo => '$::unc =~ m/^\\\\\\\\([^\\\\]+)\\\\/; # Urgle. my $server = $1;', }; __END__ Name "main::unc" used only once: possible typo at bench line 6. Benchmark: running blackadder, theorbtwo for at least 5 CPU seconds... blackadder: 5 wallclock secs ( 5.08 usr + 0.00 sys = 5.08 CPU) @ 76 +238.39/s (n=387291) theorbtwo: 6 wallclock secs ( 5.07 usr + 0.00 sys = 5.07 CPU) @ 97 +024.65/s (n=491915) Rate blackadder theorbtwo blackadder 76238/s -- -21% theorbtwo 97025/s 27% --
The reasonable small difference is what I expect. $& isn't as costly as it used to be, and since the blackadder code only has two regexes, you pay the price twice, compared to one for theorbtwo (due to the parens). The larger difference is that blackadder has to _copy_ the string, while that doesn't happen by theorbtwo.

Abigail


In reply to Re: Unfortunately benchmarking things with $& isn't easy... by Abigail-II
in thread Obtaining server name from UNC path by blackadder

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.