All pastes #821433 Raw Edit

regextest

public text v1 · immutable
·published 2007-12-18 03:05 UTC
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 });