#!/usr/bin/perl -sw use strict; use Benchmark; our $HASHSIZE ||= 1e3; our $ITERATIONS ||= 1e7; my %hash = (); # Init hash for (my $i=0; $i<$HASHSIZE; $i++) { $hash{$i} = 1; } timethese($ITERATIONS, { _keys => sub { if (keys %hash) { ; } }, _scalar_keys => sub { if (scalar(keys %hash)) { ; } }, _scalar => sub { if (scalar(%hash)) { ; } }, _if => sub { if (%hash) { ; } }, } ); __END__ =pod =head1 NAME bm_hash_emptiness.pl - benchmark hash emptiness tests. =head1 SYNOPSIS ./bm_hash_emptiness.pl -ITERATIONS=1e7 -HASHSIZE=1e4 =head1 RESULTS $ ./bm_hash_emptiness.pl -ITERATIONS=1e7 -HASHSIZE=1e4 Benchmark: timing 1e7 iterations of _if, _keys, _scalar, _scalar_keys... _if: 14 wallclock secs (14.65 usr + 0.01 sys = 14.66 CPU) _keys: 2 wallclock secs ( 1.99 usr + 0.00 sys = 1.99 CPU) _scalar: 16 wallclock secs (15.03 usr + 0.01 sys = 15.04 CPU) _scalar_keys: 2 wallclock secs ( 2.40 usr + 0.00 sys = 2.40 CPU) =head SEE ALSO perlfunc =cut