diff options
author | Gisle Aas <aas@bergen.sn.no> | 1997-02-05 14:42:49 +0100 |
---|---|---|
committer | Chip Salzenberg <chip@atlantic.net> | 1997-02-11 07:29:00 +1200 |
commit | 4d335c471cacb999603dc9b7dc6d3712051dbb6c (patch) | |
tree | 25b18fb7d338d5615e8a0bfadc4517a23e7e3873 | |
parent | 778183f34c6f9006721e20db0af0805a4bcb0227 (diff) | |
download | perl-4d335c471cacb999603dc9b7dc6d3712051dbb6c.tar.gz |
Faster File::Compare
The following optimization speeds up File::Compare with 30% on my
machine (in a test where most files in fact were different).
One could perhaps also optimize the situation where the two files are
the same thing. Is it portable to compare (stat)[0,1] for the two
files and return 0 if these two numbers are equal?
p5p-msgid: <199702051342.OAA02753@bergen.sn.no>
-rw-r--r-- | lib/File/Compare.pm | 10 |
1 files changed, 8 insertions, 2 deletions
diff --git a/lib/File/Compare.pm b/lib/File/Compare.pm index e76c10fb5f..cef2423b7f 100644 --- a/lib/File/Compare.pm +++ b/lib/File/Compare.pm @@ -27,7 +27,7 @@ sub compare { my $to = shift; my $closefrom=0; my $closeto=0; - my ($size, $status, $fr, $tr, $fbuf, $tbuf); + my ($size, $fromsize, $status, $fr, $tr, $fbuf, $tbuf); local(*FROM, *TO); local($\) = ''; @@ -42,6 +42,7 @@ sub compare { open(FROM,"<$from") or goto fail_open1; binmode FROM; $closefrom = 1; + $fromsize = -s FROM; } if (ref($to) && (isa($to,'GLOB') || isa($to,'IO::Handle'))) { @@ -54,11 +55,16 @@ sub compare { $closeto = 1; } + if ($closefrom && $closeto) { + # If both are opened files we know they differ if their size differ + goto fail_inner if $fromsize != -s TO; + } + if (@_) { $size = shift(@_) + 0; croak("Bad buffer size for compare: $size\n") unless ($size > 0); } else { - $size = -s FROM; + $size = $fromsize; $size = 1024 if ($size < 512); $size = $Too_Big if ($size > $Too_Big); } |