Also sprach Anno Siegel:
> John W. Krahn <> wrote in comp.lang.perl.misc:
>> Anno Siegel wrote:
>> > Gunnar Hjalmarsson <> wrote in comp.lang.perl.misc:
>
> [using index() instead of s///]
>
>> > my $i = length $thisPage;
>> > substr( $thisPage, $i, length $find) = $replace while
>> > ( $i = rindex $thisPage, $find, $i) >= 0;
>> >
>> > That way only the unchanged part of the string is ever searched.
>>
>> Also, using the four argument substr() should be faster.
>
> How so? I never heard of that.
John is right according to a benchmark:
#!/usr/bin/perl -w
use strict;
use Benchmark qw/cmpthese/;
my $string = "0" x 100;
cmpthese(-2, {
arg4 => sub {
substr $string, rand length $string, 1, "0";
},
arg3 => sub {
substr($string, rand length $string, 1) = "0";
},
});
__END__
Rate arg3 arg4
arg3 512250/s -- -43%
arg4 903253/s 76% --
The reason for arg4 being faster is the fact that perl needs to attach
magic to the return value of the 3-argument substr(). In the 4-argument
case this is not the case.
> I use "=" with substr() assignments because it reads better. Four argument
> substr is for when I need the old value of the substring too.
In most cases though the argument of readability supersedes speed, so
now you're right.
Tassilo
--
use bigint;
$n=71423350343770280161397026330337371139054411854 220053437565440;
$m=-8,;;$_=$n&(0xff)<<$m,,$_>>=$m,,print+chr,,while(($ m+=

<=200);