hi all,

i just saw a post on the perl beginner's list and it was prepending a
dir to a filename with the usual $x = "$dir/$x. i replied that 4 arg
substr is usually faster. i wanted to make sure so i ran a simple
benchmark on this. the code and results are below.  it is faster because
it knows how to directly manipulate a string in the perl guts. the
assign method has to do a full copy of the string and reallocate the var
buffer to do that.

#!/usr/local/bin/perl

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%     --

so my question for you all is do you use 4 arg substr? have you ever
seen it before? it is newer than 3 arg substr but it has been in perl
for a good while now. if you have any questions on this function
(actually an option on a function) post them here. i think it should be
used much more often that i see it.

this could be a good ongoing topic. post some other cool features you
think are underused in perl.

uri

-- 
Uri Guttman  ------  [email protected]  --------  http://www.sysarch.com --
-----  Perl Code Review , Architecture, Development, Training, Support ------
---------  Gourmet Hot Cocoa Mix  ----  http://bestfriendscocoa.com ---------

_______________________________________________
Boston-pm mailing list
[email protected]
http://mail.pm.org/mailman/listinfo/boston-pm

Reply via email to