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.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|