Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2024-04-23 21:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found