#!/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; }
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)
In reply to Re: Re^3: Peeling the Peelings
by bobn
in thread Peeling the Peelings
by PetaMem
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |