Thanks again,
Putting the eval into Method 2 below is (much) slower than any other match (as noted by BrowserUK). Brute force (Method 3) is significantly faster than either 1 or 2, but does not let me loop through an array to check match counts.
I have tried "grep -c" within perl and it works, but is not as fast as brute force. Is there a faster way?
After 15+ years of Perl I have not needed to worry about cutting edge optimizing (in Perl) ... ergo my ignorance.
Thanks for help again.
my $flush = 0;
my @suits = ( "D", "S", "C", "H" );
my $long_hand = ("2H 3D 4C 5S 5H)";
# Method 1: 43 microseconds
# (My initial code ... seemed slow ... ergo benchmark)
foreach ( @suits ) {
@m = ( $long_hand =~ m/$_/g );
$flush = 1 if ($#m eq 4 );
}
# Method 2: 126 microseconds (definitely bottleneck)
foreach ( @suits ) {
my $count = ( eval "\$long_hand =~ tr/$_//" );
$flush = 1 if ($count eq 5 );
}
#Method 3: accounts for only 3(!) microseconds of code
# Ugly but functional for very few patterns to check
# BUT what if I had 10k patterns to check
$flush = 1 if ( $long_hand =~ tr/D// eq 5);
$flush = 1 if ( $long_hand =~ tr/S// eq 5);
$flush = 1 if ( $long_hand =~ tr/C// eq 5);
$flush = 1 if ( $long_hand =~ tr/H// eq 5);
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.