Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

Ok. To conclude my explorations on the issue of this node, something which can be eventually useful for others, I summarized the three solutions to "get ( $1, $2, ... )" favoured in this thread and made a little benchmark on them.

There is the solution which uses @- and @+: no messing with magic $<n> variables, but needs to know about the variable it was matched against.

# @groups = groups1($s) sub groups1 { return map { substr $_[0], $-[$_], $+[$_] - $-[$_] } 1..$#- }

There is the beautiful solution by educated_foo with symbolic references:

# educated_foo sub groups2 { no strict 'refs'; return map { $$_ } 1..$#- }

And the quick solution given by demerphq:

# demerphq sub groups3 { return eval '($'.join(',$',1..$#-).')' }

which happens to use eval.

The general result for 5.8.8 looks like

s/iter demerphq @- and @- educated_foo demerphq 6.65 -- -44% -50% @- and @- 3.72 79% -- -10% educated_foo 3.34 99% 11% --

which suggests eval imposes a high performance penalty, making it half as fast as the educated_foo's version, which is followed closely by the solution using @- and @+ (but this never surpasses the former). This was tested on four 5.8.8 architectures: i386-freebsd-64int, cygwin, MSWin32-x86-multi-thread (vanilla-perl), MSWin32-x86-multi-thread.

Results has shown greater variances at other versions and architectures, like v5.8.2 built for PA-RISC1.1-thread-multi, v5.8.7 built for i686-linux-thread-multi, v5.8.4 built for MSWin32-x86-multi-thread, but the order kept the same. The winner was faster than the 'eval' version with percentages ranging from 65% to 145%. But as the interpreter development has seen a lot of changes up to the code of 5.8.8, I preferred to concentrate on 5.8.8 perls. Maybe 5.8.8 tested on different processor architectures like PA-RISC and PPC may reveal more trade-offs than this partial benchmark.

The benchmark code used was:

#!/usr/bin/perl # @groups = groups1($s) sub groups1 { return map { substr $_[0], $-[$_], $+[$_] - $-[$_] } 1..$#- } # educated_foo sub groups2 { no strict 'refs'; return map { $$_ } 1..$#- } # demerphq sub groups3 { return eval '($'.join(',$',1..$#-).')' # return '($'.join(',$',1..$#-).')' } use constant MAX_GROUPS => 5; # use < 26 which corresponds to 'z' use constant MAX_REPEAT => 20; use constant ITERATIONS => 20000; use constant RANDOM_SEED => 666; # $re = random_re($n) # a regex with $n groups qr/(a*)(b*)...(z*)/ # use $n >= 1 to avoid the subtleties of the empty regex qr// sub random_re { my $n = shift; my $re = join '', map { "($_*)" } 'a' .. chr(ord('a')+$n-1); return qr/$re/ } # $text = random_text($n) # a text which matches the random regex produced by random_re sub random_text { my $n = shift; return join '', map { $_ x int(rand(MAX_REPEAT)) } 'a' .. chr(ord( +'a')+$n-1) } sub run_code { my $sub = shift; srand(RANDOM_SEED); # always the same seed - so that it is reprodu +cible for (1..ITERATIONS) { $n = int(rand(MAX_GROUPS))+1; $re = random_re($n); $text = random_text($n); # print "n: $n, re: $re, text: $text\n"; die "not ok: $text =~ /$re/" unless $text =~ /$re/; # assertio +n @groups = $sub->($text); # print "groups 1: @groups\n"; } } #run_code(\&groups2); #exit; use Benchmark qw(cmpthese :hireswallclock ); use constant MIN_RUNTIME => 30; # benchmark minimum time: ? CPU second +s cmpthese(-MIN_RUNTIME(), { '@- and @-' => sub { run_code(\&groups1) }, 'educated_foo' => sub { run_code(\&groups2) }, 'demerphq' => sub { run_code(\&groups3) }, });

In reply to Benchmark - How to get ($1, $2, ...)? by ferreira
in thread How to get ($1, $2, ...)? by ferreira

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, 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, 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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others musing on the Monastery: (5)
    As of 2019-07-18 23:47 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found

      Notices?