in reply to Can you write a faster code to perform this task?

Running this slightly modified version of your code:

#! perl -slw use strict; use Time::HiRes qw[ time ]; my $start = time; my $count=0; while( <DATA> ) { while( /(M+)/g ) { $count++; } } print $count."\n"; printf "Took %9f secs\n", time() - $start; __DATA__ iiiiiiiiMMMMMMMMMMMooooooooooooMMMMMMMMMMiiiiiMMMMMMMMoooo iiiiiiiiMMMMMMMMMMMooooooooooooMMMMMMMMMMiiiiiMMMMMMMMoooo ... 79998 more lines as above ...

Resulted in this output:

C:\test>junk 240000 Took 0.199589 secs

0.2 of a second to count the quarter million occurrences of your search term in 80,000 lines doesn't seem excessive to me. How much time are you looking to save?


With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.