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

   JWK>  Or as:

   JWK>                 $_ = "$directory/$_" for @dirfiles[ 1 .. $#dirfiles ];

just to show another way that is usually faster for prepending a string:

        substr( $_, 0, 0, "$directory/" ) for @dirfiles[ 1 .. $#dirfiles ];

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

I'm not seeing it:

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




John
--
Any intelligent fool can make things bigger and
more complex... It takes a touch of genius -
and a lot of courage to move in the opposite
direction.                   -- Albert Einstein

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