Anyone willing to improve the benchmark?
#!/usr/bin/perl use warnings; use strict; use feature qw/state/; use Test::More tests => 4; use Benchmark qw/cmpthese/; my $length = 100_000; my $string = join q(), map int rand 10, 1 .. $length; my $mask = join q(), map int rand 2, 1 .. $length; my $result = bitwise($string, $mask); is arraywise ($string, $mask), $result, 'array'; is substrwise($string, $mask), $result, 'substr'; is packwise ($string, $mask), $result, 'pack'; is regexwise ($string, $mask), $result, 'regex'; sub bitwise { my $string = shift; state $mask; ($mask = shift) =~ y/01/\x00\xff/ unless defined $mask; my $result = $string & $mask; $result =~ tr/\x00//d; return $result; } sub arraywise { my $string = shift; state $mask = shift; my @chars = split //, $string; return join q(), @chars[grep substr($mask, $_, 1), 0 .. length $ma +sk]; } sub regexwise { my $string = shift; state $regex; unless (defined $regex) { $regex = '^' . join(q(), map $_ ? '(.)' : '.', split //, shift +) . '$'; } my $result = join q(), $string =~ m/$regex/o; return $result; } sub packwise { my $string = shift; state $mask; unless (defined $mask) { $mask = shift; my $template; while ($mask =~ /((.)\2*)/g) { $template .= (qw(x a))[$2] . length $1; } $mask = $template; } return join q(), unpack $mask, $string; } sub substrwise { my $string = shift; state $mask; my @mask; unless (defined $mask) { $mask = shift; while ( $mask =~ /0+/g ) { push @mask, [ $-[0], ( $+[0] - $-[0] ) ]; } } for (1 .. @mask) { my $replace = ''; #'*' x $mask[-$_][1]; #check to see substr $string, $mask[-$_][0], $mask[-$_][1], $replace; } return $string; } cmpthese(-3, { bitwise => sub { bitwise ($string, $mask) }, arraywise => sub { arraywise ($string, $mask) }, substrwise => sub { substrwise($string, $mask) }, packwise => sub { packwise ($string, $mask) }, regexwise => sub { regexwise ($string, $mask) }, });

Update: Added regexwise.

لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

In reply to Re^2: using bits to print part of a string by choroba
in thread using bits to print part of a string by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.