PerlScholar has asked for the wisdom of the Perl Monks concerning the following question:
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;
}
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Digest::MD5 seems to slow down script
by Ratazong (Monsignor) on Sep 15, 2010 at 10:54 UTC | |
|
Re: Digest::MD5 seems to slow down script
by moritz (Cardinal) on Sep 15, 2010 at 11:37 UTC | |
|
Re: Digest::MD5 seems to slow down script
by zwon (Abbot) on Sep 15, 2010 at 11:53 UTC | |
|
Re: Digest::MD5 seems to slow down script
by Marshall (Canon) on Sep 15, 2010 at 12:27 UTC | |
|
Re: Digest::MD5 seems to slow down script
by zentara (Cardinal) on Sep 16, 2010 at 10:02 UTC | |
|
Re: Digest::MD5 seems to slow down script
by DrHyde (Prior) on Sep 16, 2010 at 09:17 UTC |