in reply to Re^3: Masking part of a string
in thread Masking part of a string

Yes, changing the benchmark to rebuild the mask for each operation does show substrList coming out on top. Here's the amended code (I hope I've got it right this time)

use strict; use warnings; use Benchmark q{cmpthese}; my $str = q{AGACGAGTA} x 12000; my @toMask = ( 2 .. 6, 78, 506 .. 1473, 4863, 26290 .. 37907, 107889 .. 107996); my $resAndOrAndNot = andOrAndNot($str, \@toMask); my $resAndRegex = andRegex($str, \@toMask); my $resSubstrList = substrList($str, \@toMask); die qq{Inconsistent\n} unless $resAndOrAndNot eq $resAndRegex and $resAndOrAndNot eq $resSubstrList; my $rcAndOrAndNot = sub { my $ret = andOrAndNot($str, \@toMask); return; }; my $rcAndRegex = sub { my $ret = andRegex($str, \@toMask); return; }; my $rcSubstrList = sub { my $ret = substrList($str, \@toMask); return; }; cmpthese (-3, { andOrAndNot => $rcAndOrAndNot, andRegex => $rcAndRegex, substrList => $rcSubstrList, }); sub andOrAndNot { my ($str, $raToMask) = @_; my $mask = qq{\xFF} x length $str; my $x = q{x} x length $str; substr $mask, $_, 1, qq{\x00} for @$raToMask; my $masked = ($str & $mask) | ($x & ~ $mask); return $masked; } sub andRegex { my ($str, $raToMask) = @_; my $mask = qq{\xFF} x length $str; my $x = q{x} x length $str; substr $mask, $_, 1, qq{\x00} for @$raToMask; my $masked = $str & $mask; $masked =~ s{\x00}{x}g; return $masked; } sub substrList { my ($str, $raToMask) = @_; substr $str, $_, 1, q{x} for @$raToMask; return $str; }

and the output

Rate andRegex andOrAndNot substrList andRegex 12.8/s -- -43% -50% andOrAndNot 22.6/s 77% -- -11% substrList 25.4/s 99% 12% --

Cheers,

JohnGG