I have a script that performs some checks on files. The problem is that its taking hours to run when I was hoping to run it in mins. The first check compares if files exist in 2 directories using List::Compare module. The second check compares the checksums of the files using Digest::MD5 to make sure they are the same and some of you suggested this is the likely cause of my problem. Would appreciate any advice regarding this. Many thanks!
use strict;
use warnings;
use Digest::MD5 qw(md5 md5_hex md5_base64);
#2nd part of script
my %dir1 = getChksums("$dir1");
#print Dumper \%dir1;
my%dir2 = getChksums("$dir2");
#print Dumper \%dir2;
my $num_errors = 0;
foreach my $file (keys %dir1){
if (!exists ($dir2{$file}) ){
#print "file: $file doesn't exist in 2nd directory\n";
}
elsif ($dir1{$file} ne $dir2{$file}){
print "MD5 did not match for: $file\n";
$num_errors++;
}
#print "total errors = $num_errors\n";
else {
}
#print "$file\n";
}
sub getChksums {
my $path = shift;
my %file2chksum;
opendir (INDIR, $path) or die ("Error opening: $path");
my @files = grep {-f "$path/$_"}readdir INDIR;
close INDIR;
foreach my $file (@files){
open (IN, '<', "$path/$file") or die ("Error opening: $path/$file");
$file2chksum{$file} = md5_hex(<IN>);
#print "$file $file2cksum{$file}\n";
close IN;
}
return %file2chksum;
}
In reply to Digest::MD5 seems to slow down script by PerlScholar
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |