Hi Monks,
I have written a small script to process to files (which are of different file size each) the following way -
1. Open file 1 and create a hash of (key, value) pairs.
2. Open file 2, create a hash of (key, value) pairs.
3. Using the keys in Step.1, print the value from the hash created in Step.2.
To perform these three steps, I am using the following code (please excuse any inadvertent mistake, this is only my second Perl program).
#!/usr/bin/perl
use strict;
use warnings;
open(IHF, "<", "File1");
print "\n\n";
while( <IHF> ) {
my %fets;
my ($k, $v) = split;
$fets{$k} = $v if defined ($k) and defined ($v);
my @keys = sort keys %fets;
my $trgt = getval(@keys);
print "$k $fets{$k} $trgt\n";
}
sub getval {
my @k = @_;
my %vals;
open (JHF, "<", "File2");
while(<JHF>) {
my ($key, $val) = split;
$vals{$key} = $val if defined $val and defined $key;
}
for my $i (@k) {
return "$vals{$i}";
}
}
When I execute this code on File1(of size 3.4 Gb) and File2 of size (1.4 Gb), the code runs extremely slow.
Can any Monks kindly suggest anyway to improve run time performance?
I appreciate it very much.
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.