Sorry, dude, I still 0wn y0u! ;-)
#!/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 ) { /([^()]+)\)+$/; return $1 } while ( $level-- > 0) { chop; s/^[^(]+\(//; } return $_; } my @data = <DATA>; chomp @data; my $count = 25000; timethese ( $count, { 'mine-elegant' => sub { for (@data) { prop3( split ); } }, 'mine-less-elegant' => sub { for (@data) { getpropstr( split +); } }, 'original++' => sub { for (@data) { get_proparg_new( split ); + } }, 'original' => sub { for (@data) { get_proparg( split ); } }, } ); # the original sub get_proparg { my $propstr = shift; # get the property string my $level = shift || 0; # get the level we want to extract my $back = $propstr; # Initialize Return value (for case leve +l=0) my $cnt; # initialize counter if($level == -1) { # special case, get the innermost argume +nt $propstr =~ /\(([^\(\)]+)\)+/; $propstr = $1; } else { # get whatever argument $level indicates for($cnt = 0;$cnt<$level; $cnt++) { $propstr =~ /\((.+)\)/; $propstr = $1; } } return $propstr; } # slick code, but not quite so fast as the original. sub prop3 { my $str = shift; my $level = shift || 0; return $str unless $level; my @str = split(/[()]/, $str); splice @str, 0, $level; my $out = join('(', @str); $out .= ')' x ( @str - 1 ); return $out; } # 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 =~ /\(([^\(\)]+)\)+/; return $1; } else { # get whatever argument $level indicates for($cnt = 0;$cnt<$level; $cnt++) { $propstr =~ /\((.+)\)/; $propstr = $1; } return $propstr; } } __END__ hello(what(is(this(all(about))))) -1 hello(what(is(this(all(about))))) 0 hello(what(is(this(all(about))))) 1 hello(what(is(this(all(about))))) 2 hello(what(is(this(all(about))))) 3 hello(what(is(this(all(about))))) 4 hello(what(is(this(all(about))))) 5
Results:
Benchmark: timing 25000 iterations of mine-elegant, mine-less-elegant, + original, original++... mine-elegant: 8 wallclock secs ( 8.20 usr + 0.00 sys = 8.20 CPU) @ +3048.78/s (n=25000) mine-less-elegant: 6 wallclock secs ( 5.64 usr + 0.01 sys = 5.65 CP +U) @ 4424.78/s (n=25000) original: 9 wallclock secs ( 9.29 usr + 0.00 sys = 9.29 CPU) @ 26 +91.07/s (n=25000) original++: 9 wallclock secs ( 9.33 usr + 0.00 sys = 9.33 CPU) @ 26 +79.53/s (n=25000) Benchmark: timing 25000 iterations of mine-elegant, mine-less-elegant, + original, original++... mine-elegant: 9 wallclock secs ( 8.67 usr + 0.00 sys = 8.67 CPU) @ +2883.51/s (n=25000) mine-less-elegant: 8 wallclock secs ( 6.11 usr + 0.05 sys = 6.16 CP +U) @ 4058.44/s (n=25000) original: 9 wallclock secs ( 8.67 usr + 0.01 sys = 8.68 CPU) @ 28 +80.18/s (n=25000) original++: 9 wallclock secs ( 8.72 usr + 0.01 sys = 8.73 CPU) @ 28 +63.69/s (n=25000) Benchmark: timing 25000 iterations of mine-elegant, mine-less-elegant, + original, original++... mine-elegant: 9 wallclock secs ( 8.40 usr + 0.00 sys = 8.40 CPU) @ +2976.19/s (n=25000) mine-less-elegant: 6 wallclock secs ( 5.22 usr + 0.02 sys = 5.24 CP +U) @ 4770.99/s (n=25000) original: 9 wallclock secs ( 8.33 usr + 0.00 sys = 8.33 CPU) @ 30 +01.20/s (n=25000) original++: 8 wallclock secs ( 7.88 usr + 0.02 sys = 7.90 CPU) @ 31 +64.56/s (n=25000)


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

In reply to Re: Re: 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.