Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: Pattern matching across two files, Need something better than grep -f!

by kennethk (Monsignor)
on Apr 10, 2012 at 18:26 UTC ( #964384=note: print w/ replies, xml ) Need Help??


in reply to Pattern matching across two files, Need something better than grep -f!

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.


Comment on Re: Pattern matching across two files, Need something better than grep -f!
Select or Download Code
Re^2: Pattern matching across two files, Need something better than grep -f!
by Anonymous Monk on Apr 10, 2012 at 18:52 UTC
    A little bit faster:
    use strict; use warnings; open my $data_fh, '<', "data" or die $!; open my $patern_fh, '<', "pattern.txt" or die $!; my %patterns; while (<$patern_fh>) { $patterns{join $;, split} = (); } close $patern_fh; { local $, = "\t"; local $\ = "\n"; while (<$data_fh>) { my @line = split; if (exists $patterns{$line[0] . $; . $line[1]}) { print @line[0, 1, 3]; } } } close $data_fh;
Re^2: Pattern matching across two files, Need something better than grep -f!
by petdance (Parson) on Apr 10, 2012 at 19:59 UTC
    I suggest that even before going to measure the bottlenecks, the OP needs to define what is goal is. If the process is taking 10 seconds and he wants it to run in two, then mere tweaking may not be enough.

    xoxo,
    Andy

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://964384]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (8)
As of 2014-12-29 17:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (194 votes), past polls