On Tue, May 31, 2011 at 5:06 PM, Uri Guttman <[email protected]> wrote:

> the OP actually looked up the docs and found
> lvalue substr but i replied it shouldn't be used as 4 arg substr can do
> the same thing and is much faster.

I agree.  Although lvalue substr is clearer than 4-arg substr, it's
even slower than assignment.  Assignment is far clearer than the
lvalue substr in a case like this, so I don't see a good reason to use
lvalue substr.  That said, unless I needed to optimize, in most cases
I'd just stick with assignment as the clearest solution despite being
slower than 4-arg substr.

use Benchmark qw( cmpthese );

cmpthese(-2, {
    assign => sub { my $x = 'bar'; $x = "foo/$x" },
    substr => sub { my $x = 'bar'; substr $x, 0, 0, 'foo/' },
    lvalue => sub { my $x = 'bar'; substr($x, 0, 0) = 'foo/' },
                                                                  });

            Rate lvalue assign substr
lvalue 1257127/s     --   -37%   -60%
assign 2001479/s    59%     --   -36%
substr 3127850/s   149%    56%     --

--
Nick Patch
nickpatch.net

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

Reply via email to