Feb 122010
Benchmark Perl subroutines
Answer:
The Benchmark module provide a very easy way to compare (cmpthese) the performance of Perl's subroutines.
E.g. The following codes compare the performance of MD5 and SHA1 hashing methods.
#!/usr/local/bin/perl
use strict;
use Benchmark qw(cmpthese);
use Digest::MD5 qw(md5_hex);
use Digest::SHA1 qw(sha1_hex);
my $str = '83b0c3d63e8a11eb6e40077030b59e95bfe31ffa';
my $s;
cmpthese( 1_000_000, {
'md5' => sub{ $s = md5_hex($str) },
'sha1' => sub{ $s = sha1_hex($str) }
});
It will output
# perl compare_md5_sha1.pl
Rate sha1 md5
sha1 934579/s -- -39%
md5 1538462/s 65% --
Obviously, MD5 is faster than SHA1.