Strangley enough, the benchmarks indictate this is no better. comparison w/ minor rewrites:
#!/usr/bin/perl -w $|++; use strict; use Benchmark; # # not so elegant but still the chanp. sub getpropstr { local $_ = shift; my $level = shift || 0; if ( $level == -1 ) { /([^()]+)\)+$/o; return $1 } while ( $level-- > 0) { chop; s/^[^(]+\(//o; } return $_; } my @data = <DATA>; chomp @data; my $count = 7000; timethese ( $count, { 'my best: getpropstr' => sub { for (@data) { getpropstr( split + ); } }, 'OP best: get_proparg_new' => sub { for (@data) { get_proparg +_new( split ); } }, 'orig-arg2' => sub { for (@data) { arg2( split ); } }, 'yours' => sub { for (@data) { arg3( split ); } }, } ); sub arg2 { my($str,$lvl)= @_; my $str1 = $str; return $str if !$lvl; if ( $lvl == -1 ) { $str =~ /\(([^\(\)]+)\)+/; return $1; } $str =~ /([^(]*\(){$lvl}(.+)(\)){$lvl}/; return $2; } sub arg3 { my($str,$lvl)= @_; my $str1 = $str; return $str if !$lvl; if ( $lvl == -1 ) { $str =~ /\(([^\(\)]+)\)+/; return $1; } $str =~ /\A (?> (?> [^(]* \( ) {$lvl} ) ( .+ ) \) {$lvl} \z/x; return $1; } # the original poster's improved version sub get_proparg_new { my $propstr = shift; # get the property string my $level = shift || return $propstr; # get the level we want to e +xtract my $cnt; # initialize counter if($level == -1) { # special case, get the innermost argume +nt $propstr =~ /\(([^\(\)]+)\)+/o; return $1; } else { # get whatever argument $level indicates for($cnt = 0;$cnt<$level; $cnt++) { $propstr =~ /\((.+)\)/o; $propstr = $1; } return $propstr; }
Results:
Benchmark: timing 7000 iterations of OP best: get_proparg_new, my best +: getpropstr, orig-arg2, yours... OP best: get_proparg_new: 3 wallclock secs ( 2.39 usr + 0.01 sys = +2.40 CPU) @ 2916.67/s (n=7000) my best: getpropstr: 4 wallclock secs ( 1.72 usr + 0.01 sys = 1.73 +CPU) @ 4046.24/s (n=7000) orig-arg2: 12 wallclock secs ( 9.71 usr + 0.04 sys = 9.75 CPU) @ 71 +7.95/s (n=7000) yours: 12 wallclock secs ( 9.69 usr + 0.03 sys = 9.72 CPU) @ 72 +0.16/s (n=7000)


--Bob Niederman, http://bob-n.com

In reply to Re: Re^3: Peeling the Peelings by bobn
in thread Peeling the Peelings by PetaMem

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.