Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: Faster way to do this?

by ctilmes (Vicar)
on Nov 12, 2015 at 16:31 UTC ( [id://1147587]=note: print w/replies, xml ) Need Help??


in reply to Faster way to do this?

If you put the match in list context with (), it will return the matches which you can count and discard with a scalar context like this:
$count = () = /(M+)/g;

Replies are listed 'Best First'.
Re^2: Faster way to do this?
by stevieb (Canon) on Nov 12, 2015 at 16:39 UTC

    I thought that too, but it isn't faster:

    use warnings; use strict; use Benchmark qw(:all); open my $fh, '<', $ARGV[0] or die $!; cmpthese(100, { all => '_all', while => '_while', }); sub _all { seek $fh, 0, 0; my $count = 0; while (<$fh>){ $count += () = /(M+)/g; } # print "all: $count\n"; } sub _while { seek $fh, 0, 0; my $count=0; while(<$fh>){ while($_=~/(M+)/g){ $count++; } } # print "while: $count\n"; } __END__ Rate all while all 11.5/s -- -25% while 15.2/s 33% --

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (3)
As of 2024-04-16 03:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found