Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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:

  1. 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).
  2. 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.
  3. 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: $!";
  4. 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.


In reply to Re: Pattern matching across two files, Need something better than grep -f! by kennethk
in thread Pattern matching across two files, Need something better than grep -f! by anasuya

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • 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.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2024-04-23 22:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found