Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: Faster way to do this?

by ctilmes (Vicar)
on Nov 12, 2015 at 16:31 UTC ( #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? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (1)
As of 2021-09-28 17:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?