Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Array loop very inefficient, 100% CPU usgage! 1

Status
Not open for further replies.

waiterm

Programmer
May 17, 2004
236
GB
Hiya guys,

I'm sure this is a very simple on for someone, I have a loop to compare the data in two arrays. Whenever I run the sub routine, however, the CPU usage shoots up to 100% and I was wondering if anyone had any suggestions as to why the sub routine was so inefficient.
Code:
sub compare_avs  {
	
	open(DIFF_OLD, $path.$agent."/AVS/DIFF_OLD.txt") || die "ERROR: Unable to open ".$agent." - DIFF_OLD_FILE.txt";
	$DIFF_OLD = <DIFF_OLD>;
	close DIFF_OLD;


	
	if ($DIFF_OLD eq "")  {
		print "DIFF_OLD file - Does not exist\n"; sleep(5);
		open(DIFF, "$EMAILfile") || die "ERROR: Unable to open ".$agent." - ".$EMAILfile;
		while(my @AVSn = split/\n/, <DIFF>) {
			foreach $AVn(@AVSn) {
				$data = $data.$AVn."\n";
			}
		}
		close DIFF;
	}  else  {
		print "DIFF_OLD file - ".$DIFF_OLD."\n"; sleep(5);
		
		open(CHECK, $path.$agent."/AVS/".$DIFF_OLD) || die  "ERROR: Unable to open ".$agent." - ".$DIFF_OLD;
		@AVSo = <CHECK>;
		close CHECK;
		
		open(DIFF, "$EMAILfile") || die "ERROR: Unable to open ".$agent." - ".$EMAILfile;
		while(my @AVSn = split/\n/, <DIFF>) {
			foreach $AVn(@AVSn) {
				$AVSchecked = 0;
				($SIDn,$DATEn,$AVSn,$NOTHINGn,$EPOCHDATEn,$SERIALn) = split/::/,$AVn;
				foreach $AVo(@AVSo)  {
					$AVo =~ s/\n|\r//g;
					($SIDo,$DATEo,$AVSo) = split/::/,$AVo;
					if ($SIDn eq $SIDo && $DATEn eq $DATEo && $AVSn ne $AVSo)  {
						$data = $data.$SIDn."::".$DATEn."::".$AVSn."::::".$EPOCHDATEn."::".$SERIALn."\n";
						print $SIDn."::".$DATEn."::".$AVSn."::::".$EPOCHDATEn."::".$SERIALn."\n";
						$AVSchecked = 1;
					}  elsif  ($SIDn eq $SIDo && $DATEn eq $DATEo && $AVSn eq $AVSo)  {
						$AVSchecked = 1;
					}
				}
				if ($AVSchecked == 0)  {
					$data = $data.$SIDn."::".$DATEn."::".$AVSn."::::".$EPOCHDATEn."::".$SERIALn."\n";
					print $SIDn."::".$DATEn."::".$AVSn."::::".$EPOCHDATEn."::".$SERIALn."\n";
				}
			}
		}
		close DIFF;
	}
	
	$DIFF_OLD_filex = $path.$agent."/AVS/DIFF_OLD.txt";
	open(DIFF_OLD,">".$path.$agent."/AVS/DIFF_OLD.txt") || die "ERROR: Unable to open ".$agent." - DIFF_OLD_FILE.txt to update";
	print DIFF_OLD $agent.$$.".AVS";
	close DIFF_OLD;
	
}
Here's a sample of the data in each array element:
Code:
101210::20041009::YYYYYYY::::1096930741::_1_
101210::20041016::YYYYYYY::::1096930741::_1_
101210::20041023::NNNNNNN::::1096930741::_1_
101210::20041030::NNNNNNN::::1096930741::_1_
101210::20041106::NNNNNNN::::1096930741::_1_
101212::20041009::NNNNNNN::::1096930741::_2_
101212::20041016::NNNNNNN::::1096930741::_2_
101212::20041023::NNNNNNN::::1096930741::_2_
101212::20041030::YYYYYYY::::1096930741::_2_
101212::20041106::YYYYYYY::::1096930741::_2_
etc...etc...

Rob Waite
 
Hi Rob,

Have you thought of reading $path.$agent."/AVS/".$DIFF_OLD into %AVSo instead of @AVSo?

A hash instead of an array in other words.

The advantage of this is that searching for a particular entry becomes *much* more efficient. You do have to pick a unique key for %AVSo though.

Mike

"Deliver me from the bane of civilised life; teddy bear envy."

Want to get great answers to your Tek-Tips questions? Have a look at faq219-2884

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top