Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: most terse regex match count

by haukex (Archbishop)
on May 16, 2019 at 15:32 UTC ( [id://11100101]=note: print w/replies, xml ) Need Help??


in reply to most terse regex match count

Is it possible to force list context in a conditional?

A variation of the "Saturn" operator does the trick: if ( 2 == ( () = $outs =~ m|A|gs ) )

Replies are listed 'Best First'.
Re^2: most terse regex match count
by Veltro (Hermit) on May 17, 2019 at 14:38 UTC

    Quick thought to reduce capturing: m|()A|gs? If I understand docu correctly the list is populated and then the values are discarded. So collect the smallest possible value instead.

      Quick thought to reduce capturing: m|()A|gs?

      Unfortunately it seems the extra capture group actually slows things down - no matter how much I fiddle with the parameters at the top, nocapt is always about 2x as fast:

      #!/usr/bin/env perl use warnings; use strict; use Benchmark qw/cmpthese/; my $count = 1000; my $match = 'ABC'x20; my $space = 'xAz'x100; my $str = ( $space.$match x $count ) . $space; cmpthese(-3, { nocapt => sub { die unless $count == ( () = $str =~ m|\Q$match|gs ); }, withcapt => sub { die unless $count == ( () = $str =~ m|()\Q$match|gs ); }, }); __END__ Rate withcapt nocapt withcapt 4264/s -- -50% nocapt 8596/s 102% --

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11100101]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (7)
As of 2024-04-23 19:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found