>>>>> "JWK" == John W Krahn <jwkr...@shaw.ca> writes:

  JWK> Uri Guttman wrote:
  >>>>>>> "JWK" == John W Krahn<jwkr...@shaw.ca>  writes:
  >> 
  >> that is called 4 arg substr and it is way underused in perl. this
  >> benchmark shows the significant speedup of about 2x:
  >> 
  >> 
  >> use Benchmark qw( cmpthese ) ;
  >> 
  >> cmpthese ( shift || -2, {
  >> 
  >> assign =>  sub { my $x = 'abc' ; $x = "qwerty/$x" },
  >> substr =>  sub { my $x = 'abc' ; substr( $x, 0, 0, 'qwerty/') },
  >> }
  >> ) ;
  >> 
  >> 
  >> Rate assign substr
  >> assign 2356409/s     --   -47%
  >> substr 4428292/s    88%     --

  JWK> I'm not seeing it:

  JWK> $ perl -le'
  JWK> use Benchmark qw( cmpthese );
  JWK> cmpthese ( shift || -2, {
  JWK>     assign => sub { my $x = "abc"; my $dir = "qwerty"; $x = "$dir/$x" },
  JWK>     substr => sub { my $x = "abc"; my $dir = "qwerty"; substr $x, 0,
  JWK> 0, "$dir/" },
  JWK>     } );
  JWK> '

  JWK>             Rate substr assign
  JWK> substr  997968/s     --    -2%
  JWK> assign 1018371/s     2%     --

you are doing more work than my version. so i benchmarked all 4
variations and this is the result.


cmpthese ( shift || -2, {

                assign => sub { my $x = 'abc' ; $x = "qwerty/$x" },
                substr => sub { my $x = 'abc' ; substr( $x, 0, 0, 'qwerty/') },
                assign2 => sub { my $x = 'abc' ; my $z = 'qwerty' ;
                        $x = "$z/$x" },
                substr2 => sub { my $x = 'abc' ;  my $z = 'qwerty' ;
                        substr( $x, 0, 0, "$z/" ) },
        }
) ;

             Rate assign2 substr2  assign  substr
assign2 2383123/s      --     -1%    -13%    -39%
substr2 2407127/s      1%      --    -12%    -38%
assign  2739050/s     15%     14%      --    -30%
substr  3896373/s     63%     62%     42%      --

what i see is that the extra assignment to $z is making that the largest
part of the cpu load and hides the difference of the substr vs
assignment. one key to benchmarking is to isolate the actual code you
want to compare. in this case there are two plain assignments which
overwhelm the difference on the code under comparison. my original
benchmark (show here again) highlights the difference better. if i could
get rid of the needed initialization assignment i would.

note that this benchmark will also be affected by the length of the
strings involved (both the initialized and the prepended). you can
likely design a set of strings that will make either look faster than
the other.

uri

-- 
Uri Guttman  ------  u...@stemsystems.com  --------  http://www.sysarch.com --
-----  Perl Code Review , Architecture, Development, Training, Support ------
---------  Gourmet Hot Cocoa Mix  ----  http://bestfriendscocoa.com ---------

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to