in reply to Re^3: Simple Matching
in thread Simple Matching
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);
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^5: Simple Matching
by AnomalousMonk (Archbishop) on Sep 28, 2009 at 12:42 UTC | |
by ikegami (Patriarch) on Sep 28, 2009 at 15:53 UTC | |
|
Re^5: Simple Matching
by BrowserUk (Patriarch) on Sep 29, 2009 at 00:54 UTC |