rendered paste body#!/usr/bin/perl
use warnings;
use strict;
use Benchmark qw(cmpthese);
sub str_replace {
my ($str, $to_match, $replace_with) = @_;
my $pos = 0;
my ($lm, $lr) = map { length; } ($to_match, $replace_with);
while ( (my $i = index($str, $to_match, $pos)) != -1) {
substr $str, $i, $lm, $replace_with;
$pos = $i + $lr;
}
return $str;
}
sub str_replace2 {
my ($str, $to_match, $replace_with) = @_;
$str =~ s/$to_match/$replace_with/g;
return $str;
}
my $str_main = <<EOF;
Title
Date
Time
Textblahblah
More text
EOF
sub a { print str_replace($str_main, "\n\n", ''); }
sub b { print str_replace2($str_main, "\n\n", ''); }
cmpthese(1000000, { 'a' => \&a, 'b' => \&b });