in reply to Re^3: Ways to delete start of string
in thread Ways to delete start of string

'purport'! You are suggesting that I'm not testing?

No. I should have said: "That your benchmark purports to be testing". (Where 'your' refered to ikegami). The point is that it looks like it's testing the right thing, but when you factor in the overhead of the tests, it serves to completely obscure the results.

Like others, I think this is a fairly fruitless test. If you are doing this once, then the method used will make very little difference. If you are doing it hundreds of thousands of times, then there are far more efficient ways of doing it. For example,

  1. if the idea is to chip off characters until you reach a particular character, then search for the character and then chop the lump off:
    ## Either substr( $x, 0, index( $x, $char ), ''); ## or $x =~ s[(^/*$char)][];
  2. if the requirement is to remove and process the first N chars of a string, then chop off the lump then separate them:
    for my $char ( unpack '(A1)*', substr $x, 0, 100, '' ) { ## }

The only time the relative performance of these methods is likely to make a significant difference is if you were applying it once to each of a large number of strings, say a large array, much as you might with chop( @array ). And it would have to be in the order of 10e7 elements before it would have any significant effect upon an application. If it was a common requrement, then there would probably be a leading character equivalent of chop (chip()?:) built-in.

Could you give an example of 'add a multiplier loop inside the subroutine'?

This was how I constructed my variation:

use strict; use warnings; use Benchmark qw( cmpthese ); my %tests = ( subst => '$x =~ s/.//;', substr_lval => 'substr($x,0,1) = "";', substr_mod => 'substr($x,0,1,"");', reverse => '$x = reverse $x; chop($x); $x = reverse($x);', substr_copy => '$x = substr($x,1);', ); our $loops ||= 1e4; for (values %tests) { $_ = <<EOT my \$x = 'X' x $loops; for( 1 .. $loops ) { $_ } EOT } cmpthese(-3, \%tests);

Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
"Too many [] have been sedated by an oppressive environment of political correctness and risk aversion."

Replies are listed 'Best First'.
Re^5: Ways to delete start of string
by hsmyers (Canon) on May 28, 2008 at 02:03 UTC
    I agree that there is little to be gained in such an optimization. That said, I've made out like a bandit at the 'meta' level by posting the article itself. Not the least of which are the pieces you've posted. Much thanks. Please note that I did not start my usual rant about how optimization is always a good thing. Be grateful.

    --hsm

    "Never try to teach a pig to sing...it wastes your time and it annoys the pig."