#!/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 = ; 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 level=0) my $cnt; # initialize counter if($level == -1) { # special case, get the innermost argument $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 extract my $cnt; # initialize counter if($level == -1) { # special case, get the innermost argument $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