use strict; use warnings; use 5.020; use String::Random 'random_string'; use Benchmark 'cmpthese'; my $s = random_string( 'b' x 10000 ); my %h = map { chr, chr( $_ + 30 + rand 10) } 0 .. 29; my $re = join '|', map { quotemeta( $_.$h{$_} ) } keys %h; my ( $s1, $s2 ); cmpthese( -5, { 1 => sub { $_ = $s; s/([\x00-\x1D])(.)(?(?{ $2 ne $h{$1} })(*FAIL))/xx/sg; $s1 = $_ }, 2 => sub { $_ = $s; s/$re/xx/sg; $s2 = $_ }, } ); say 'OK' if $s1 eq $s2; __END__ Rate 1 2 1 1028/s -- -91% 2 11069/s 977% -- OK