Hi All,

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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.