[Omaha.pm] benchmark and md5sum

Dave M dave.nerd at gmail.com
Sun May 14 08:44:45 PDT 2006


I often use the same md5 subroutine in all my scripts for various
reasons. Recently I saw another way to read it in, and it appears to
be faster:

#!/usr/bin/perl

use strict;
use warnings;
use Benchmark qw/cmpthese/;
use Digest::MD5 qw/md5_hex/;
$|++;

my $dir   = '/some/dir';
my @files = glob "$dir/*";

cmpthese(
    10,
    {   my_way => sub {
            foreach my $file (@files) {
                next if -d $file;
                open( FH, "<", $file ) or die "blah: $!\n";
                my $slurp  = do { local $/; <FH> };
                my $md5sum = Digest::MD5::md5_hex($slurp);

                # print "md5sum for $file = $md5sum\n";
                close(FH);
            }
        },
        new_way => sub {
            foreach my $file (@files) {
                next if -d $file;
                open( FH, "<", $file ) or die "blah: $!\n";
                binmode(FH);
                my $md5sum = Digest::MD5->new->addfile(*FH)->hexdigest;

                # print "md5sum for $file = $md5sum\n";
                close(FH);
            }
            }
    }
);

Which results in this:

                     s/iter    my_way other_way
my_way      2.04        --            -45%
new_way   1.12       82%        --

So, besides the fact that I suffer from code-reuse, am I reading this
right? I'm not a benchmark guru, but I understand that the results are
displayed from slowest to fastest...

Dave M


More information about the Omaha-pm mailing list