So anytime you want to "minimise the runtime... code to as small as possible", you need to study the code to determine where you are spending your time. This means profiling and benchmarking. I would recommend you check out
Devel::NYTProf and
Benchmark.
A couple things you are doing, which you could address, some of which would likely improve performance and others would are good coding practice, include:
- Don't slurp the whole files into memory. If you operate on one line at time, you won't chew up huge amounts of memory (8GB + data overhead).
- You reparse the entirety of your pattern file on each loop. Instead, parse once and store it in a hash. Then you can use the fast look-up a hash offers you.
- You should probably also get in the habit of testing if your opens succeed, a la open AS, "data" or die $!;, or even better open $as, '<', "data" or die "data open failed: $!";
- Consider adding strict; give Use strict warnings and diagnostics or die a read.
Implementing all this might result in something like (untested):
use strict;
use warnings;
open my $as, '<', "data" or die "data open failed: $!\n";
open my $aq, '<', "pattern.txt" or die "pattern.txt open failed: $!\n"
+;;
my %pattern;
while (<$aq>) {
my @split = split;
$pattern{"$split[0] $split[1]"} = 1;
}
while (<$as>) {
my @split = split;
if ($pattern{"$split[0] $split[1]"}) {
print "$split[0]\t$split[1]\t$split[3]\n";
}
}
#11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.
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.