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