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,
## Either substr( $x, 0, index( $x, $char ), ''); ## or $x =~ s[(^/*$char)][];
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);
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^5: Ways to delete start of string
by hsmyers (Canon) on May 28, 2008 at 02:03 UTC |