Question : Optimize code.

I've the following code which parse 2 text file, reading it record by record, and sorting them. Records are sorted by date in each file. For example :
file 1 - 1.1, 1.2, 1.3, 1.5, 1.8, 2.0, 2.3
file 2 - 1.1, 1.4, 2.0, 2.2, 2.4
give :
file 3 - 1.1, 1.1, 1.2, 1.3, 1.4, 1.5, 1.8, 2.0, 2.0, 2.2, 2.3, 2.4
OK??( hard to explain, but easy to understand!!)
I'd like to know if the following code could be optimized as parsed files are quite big.

      open(INA,"detail2");
        open(INB,"detail3");
        open(OUT,">detail4");
      
      $/="";
      
      $record2=;
      while ($record1 = ) {
            $dateA=get_date($record1);
            $dateB=get_date($record2);
            while (($dateA lt $dateB) && ($dateA !="")) {
                  print OUT $record1;
                  $dateA = get_date($record1=);
            }
            while (($dateB lt $dateA) && ($dateB !="")) {
                  print OUT $record2;
                  $dateB = get_date($record2=);
            }
      }

        close(INA);
        close(INB);
        close(OUT);

Is ask another question to convert it in C..
Thanks in advance.

Answer : Optimize code.

I am not sure, but it looks like you code has a bug. What happens when dateA == dateB? I don't think your record will get printed at all in such a case. Also, what happens if you finish reading from but still has data in it?

Also, the first "$dateB=get_date.." is a loop invariant.

Other than that I think the only place to optimize would be your get_date() subroutine. If you can move that code directly into the while loop you might see a performance boost.

Anyway, this snippet has my suggestions:
--
$record2=;
$dateB=&get_date($record2);

while ($record1 = ) {
  $dateA=&get_date($record1);
  while (($dateB <= $dateA) && ($dateB !="")) {
    print $record2;
    $dateB = &get_date($record2=);
  }
  print $record1;
}
if (($dateB > $dateA)) {
  print $record2;
}
while () {
  print;
}
--
Random Solutions  
 
programming4us programming4us