tart has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I got manifest which holds programs name and its checksum, In below code I got two hash which I build
parsing two version of manifest. I am doing below listed task to compare and findout the program changes
Manifest got: 27942 lines
Time taken: 5 min
sub compareProgram { my $manifest1HR = shift; my $manifest2HR = shift; my $main1AR = $manifest1HR->{MAIN}; my $main2AR = $manifest2HR->{MAIN}; foreach my $attr1HR(@$main1AR) { next unless($attr1HR->{Program}); foreach my $attr2HR(@$main2AR) { next unless($attr2HR->{Program}); if($attr1HR->{Program} eq $attr2HR->{Program} and $attr1HR->{File} eq $attr2HR-> +{File}) { if($attr1HR->{Size} ne $attr2HR->{Size}) { print "1: $attr1HR->{Program}\n"; print "1: $attr1HR->{File}\n"; print "1: $attr1HR->{Size}\n"; print "2: $attr2HR->{Size}\n"; } last; } } } }

Above code work perfectly but the problem is its bit slow is there any way I can change the code to make it more faster for doing same taks?
Looking forward for the suggestion.

Regards,
tart

Updates...

After parsing manifest $manifest1HR in above code contain something like this,
$VAR1 = { 'MAIN' => [ { 'Program' => 'EXECUTE', 'Size' => '1302267', 'File' => 'help.txt' }, { 'Program' => 'RUN', 'Size' => '15042454', 'File' => 'help.txt' }, { 'Program' => 'RUN', 'Size' => '387469', 'File' => 'run.exe' }, { 'Program' => 'PIPE', 'Size' => '34010402', 'File' => 'run.exe', } ] }

Replies are listed 'Best First'.
Re: Slow Comparison
by GrandFather (Saint) on Nov 30, 2010 at 03:25 UTC

    I'd turn at least one of those arrays into a hash and eliminate the nested for loop. If you provide a little sample data (or even better show small samples of the manifest files) and the expected output you'll probably even get some real code.

    True laziness is hard work
      Please check Updates! in First post, Thanks.

        Consider:

        use strict; use warnings; my $man1 = <<MAN; EXECUTE 1302267 help.txt RUN 387469 help.txt PIPE 34010402 run.exe MAN my $man2 = <<MAN; EXECUTE 1302267 help.txt RUN 387469 run.exe PIPE 34010202 run.exe MAN my %manLu1; open my $inMan, '<', \$man1; while (<$inMan>) { chomp; my ($program, $size, $file) = split ' ', $_, 3; $manLu1{$program}{$file} = $size if defined $file; } close $inMan; open $inMan, '<', \$man2; while (<$inMan>) { chomp; my ($program, $size, $file) = split ' ', $_, 3; next if ! exists $manLu1{$program}{$file} || $manLu1{$program}{$fi +le} == $size; print "1: $program\n"; print "1: $file\n"; print "1: $manLu1{$program}{$file}\n"; print "2: $size\n"; } close $inMan;

        Prints:

        1: PIPE 1: run.exe 1: 34010402 2: 34010202
        True laziness is hard work