Hello Mbedish,
Tuesday, August 07, 2001, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:
Mac> Is there a better way to compare large files than this snippet,
Mac> which runs out of memory if files > 30mb.
Mac> It is also slow, about the same speed as comparing in a text editor!
Mac> Thank you.
Mac> __SNIP__
Mac> @file1 = (<IN1>);
Mac> @file2 = (<IN2>);
first, it is a very bad practice to read entire file into memory. for
example, if file size is bigger than free memory, you'll have unpredictable
result.
second, the slowest operation in your example is file I/O. your files
may have differences in second string, so you do not need to read rest of
file.
Mac> $are_equal = compare_arrays(\@file1, \@file2);
Mac> if ($are_equal) {
Mac> print "Files are IDENTICAL \n";
Mac> }
Mac> else
Mac> {
Mac> print "Files are DIFFERENT \n";
Mac> }
Mac> sub compare_arrays {
Mac> my ($first, $second) = @_;
Mac> #no warnings; # silence spurious -w undef complaints
third, you compare your arrays two times
first pass:
Mac> return 0 unless @$first == @$second;
second pass:
Mac> for (my $i = 0; $i < @$first; $i++) {
Mac> return 0 if $first->[$i] ne $second->[$i];
Mac> }
Mac> return 1;
Mac> }
Mac> __SNIP__
so, i can recommend something like
$retval=1;
while( $a=<IN1> )
{
$b=<IN2>;
if ($a ne $b)
{
$retval=0; last;
}
}
close IN1; close IN2;
return $retval;
at the beginning, you can compare files size (perldoc -f stat).
Best wishes,
Maxim mailto:[EMAIL PROTECTED]
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]