parse hash by array element seems to run slow

From: JRoot (awkster_at_yahoo.com)
Date: 12/04/04


Date: 3 Dec 2004 19:23:34 -0800

Below I posted my program in it's entirity and I hope that is within
the guidelines ...
The program works perectly but it seems to run slower than I had
expected based on other perl programs I've authored with much more
data involved.

I've been through the faq's and every web site that focuses on perl
and to be honest, I'm overwhelmed by the number of ways it's possible
to use perl so I'm asking if anyone could just look over this program
and see if it is obvious where the overhead lies so I can remove the
baggage and speed it up a bit. It runs in about 3-5 seconds on a 1 ghz
machine but my gut tells me it could be faster or more efficient if
you will. I'm not asking anyone to do my work for me ... just for a
pointer from anyone that deals with arrays and hashes a lot.

The roadmap is ...

I create an array from a file (one word per line)

I create a hash from a 2nd file, combining duplicate key words and
assign the duplicate quantity as the value. A key-val pair would look
like ... net_name 5

I iterate the array and search the hash keys to find a match and then
report the key and val to different reports depending on the val
(quantity) which is either 0 (not found) 1 (single occurance) or
multiple which need to be verified.

I suspect I've arrayed and hashed one too many times when it could
have been done in a more simple manner.

Any help is appreciated -- thank you

snip<------ cut here -------->snip

#!/app/util/perl/bin/perl -w

BEGIN { push(@INC, "/app/util/perl5.6.1/lib/5.6.1") }

use strict;

my @nets = ();
my %count = ();
my %list = ();
my ($net, $x, $y, $netName, $side, $junk, $line, $checkHashVal);
my ($sortedNet, $tpList, $key, $val, $list, $checkHashKey, $found);
my ($oneArr, $verifyArr, $zeroArr, $line2Push);
my $lineCnt = 0;
my @tpList = ();
my @sortedNets = ();
my @zeroArr = ();
my @oneArr = ();
my @verifyArr = ();
my $cnt = 0;

print "\n\n... Working .... \n\n";

open (TL, "tp_list") || die "Can't open tp_list for reading: $!";

while(<TL>){
        chomp($_);
        push(@tpList, $_);
}
close(TL);

open (TC, "testpoint_chart") || die "Can't open testpoint_chart for
reading: $!";

while(<TC>){
        $line = $_;
        ++$lineCnt;
        if($lineCnt > 9){
                ($x, $y, $netName, $side, $junk) = split(' ', $line, 5);
                if($side eq "Top" || $side eq "Bottom"){
                        push(@nets, $netName);
                }
        }
}

close(TC);

@sortedNets = @nets;

foreach $sortedNet(@sortedNets) {
        $count{$sortedNet} = ++$count{$sortedNet};
}

foreach $sortedNet (keys %count) {
        ($key, $val) = ($sortedNet, $count{$sortedNet});
        $list{$key} = $val;
}

foreach $tpList (@tpList) {
        $found = "no";
        ++$cnt;
        foreach $key (keys %list) {
                    if ($key eq $tpList) {
                            $found = "yes";
                            $line2Push = "ARR: $key -- HASHKEY: $tpList VAL:
$list{$tpList}\n";
                            if($list{$tpList} eq "1"){
                                    push(@oneArr, $line2Push);
                            }
                            elsif($list{$tpList} ne "1"){
                                    push(@verifyArr, $line2Push);
                            }
                              last;
                    }
            }
            if($found eq "no"){
                    push(@zeroArr, $tpList);
            }
}
print "\n===== ONES =====\n";
print "------------------\n";
foreach $oneArr(@oneArr){print "ONES: $oneArr";}
print "\n===== VERIFY =====\n";
print "--------------------\n";
foreach $verifyArr(@verifyArr){print "VERIFY: $verifyArr";}
print "\n===== ZERO =====\n";
print "--------------------\n";
foreach $zeroArr(@zeroArr){print "ZERO: $zeroArr\n";}

print " ... D O N e ... \n\n";



Relevant Pages

  • [DGBI] oops, obviously Ive missed something (was: regexp or array?)
    ... foreach { ... Better use an array of precompiled regexpes: ... perl -wle ' ... use Benchmark qw|cmpthese timethese|; ...
    (comp.lang.perl.misc)
  • Re: math formula substitution and evaluation
    ... (Brackets return a reference to an anonymous array containing the list, ... converting the character to its ascii value and ... foreach my $test { ... If the tests really do look like what you've displayed, then the easiest way would be to make sure your tests are proper perl code, and then run them through "eval". ...
    (perl.beginners)
  • Re: read and process a file each 3 lines
    ... I am trying to write a second script, in perl, that would print the ... above-mentionned array, and then reckon each value encountered in my ... foreach my $i ...
    (comp.unix.shell)
  • Re: foreach statement output
    ... know about foreach loop workings. ... I've got an incoming array of unknown ... The Perl Cookbook ... My big confusion over this foreach issue is I failed to realize that the ...
    (comp.infosystems.www.authoring.cgi)
  • Re: Learning Perl
    ... it should be an array, ... Then they'd be completely inaccessible to beginners. ... that should be my $var. ... so why is it redundant to point out that Perl is different from C here? ...
    (comp.lang.perl.misc)