andreas1234567 has asked for the wisdom of the Perl Monks concerning the following question:
To my surprise Benchmark, says that if(keys %hash) is roughy 5 times faster than if(%hash).if (%a_hash) { print "has hash members\n" }
#!/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_key +s... _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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Benchmark of hash emptiness test
by GrandFather (Saint) on Nov 09, 2006 at 10:43 UTC | |
|
Re: Benchmark of hash emptiness test
by dk (Chaplain) on Nov 09, 2006 at 22:49 UTC | |
by aufflick (Deacon) on Nov 10, 2006 at 00:46 UTC | |
by demerphq (Chancellor) on Nov 10, 2006 at 10:34 UTC | |
by Hofmator (Curate) on Nov 10, 2006 at 10:55 UTC | |
by demerphq (Chancellor) on Nov 10, 2006 at 13:02 UTC | |
by aufflick (Deacon) on Nov 12, 2006 at 12:12 UTC | |
|
Re: Benchmark of hash emptiness test
by graff (Chancellor) on Nov 09, 2006 at 17:25 UTC |