Linux Ask!

Linux Ask! is a Q & A web site specific for Linux related questions. Questions are collected, answered and audited by experienced Linux users.

Feb 122010

Benchmark Perl subroutines


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.


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

Rate sha1  md5
sha1  934579/s   -- -39%
md5  1538462/s  65%   --

Obviously, MD5 is faster than SHA1.

 Leave a Reply



You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>