JupiterHost.Net wrote:
In benchmarking some code I've come across something I did not expect:

slice:
use strict;
use warnings;
my @k=qw(1 2 3 4 5 6);
my %n;@[EMAIL PROTECTED] = @k;
print "hi" if exists $n{1};
print "hi" if exists $n{3};
print "hi" if exists $n{5};
print "hi" if exists $n{7};
print "hi" if exists $n{9};
print "hi" if exists $n{11};

grep:
use strict;
use warnings;
my @k=qw(1 2 3 4 5 6);
print "hi" if grep /^1$/, @k;
print "hi" if grep /^3$/, @k;
print "hi" if grep /^5$/, @k;
print "hi" if grep /^7$/, @k;
print "hi" if grep /^9$/, @k;
print "hi" if grep /^11$/, @k;



Benchmark: timing 5000000 iterations of grep, slice...
grep: 3.65945 wallclock secs ( 2.33 usr + 0.04 sys = 2.37 CPU) @ 2109704.64/s (n=5000000)
slice: 2.37966 wallclock secs ( 2.52 usr + -0.01 sys = 2.51 CPU) @ 1992031.87/s (n=5000000)
Rate slice grep
slice 1992032/s -- -6%
grep 2109705/s 6% --


I would've thought the "slice and then use exists" would have been faster then "greping the entire array each time and using regexes" when you check it. but its consistently faster by an average 6-10%

Any ideas why that might be?

Well, let's see:


#!/usr/bin/perl use warnings; use strict; use Benchmark 'cmpthese';


my @k = qw( 1 2 3 4 5 6 );


cmpthese( -10, { exists => sub { my $count = 0; my %hash; @hash{ @k } = (); for my $num ( 1, 3, 5, 7, 9, 11 ) { $count++ if exists $hash{ $num }; } return $count; }, hash => sub { my $count = 0; my %hash = map { $_ => 1 } @k; for my $num ( 1, 3, 5, 7, 9, 11 ) { $count++ if $hash{ $num }; } return $count; }, grep_r => sub { my $count = 0; my @array = @k; for my $num ( 1, 3, 5, 7, 9, 11 ) { $count++ if grep /^$num$/, @array; } return $count; }, grep_e => sub { my $count = 0; my @array = @k; for my $num ( 1, 3, 5, 7, 9, 11 ) { $count++ if grep $_ == $num, @array; } return $count; }, } );


Produces the result (on my computer):

          Rate grep_r grep_e   hash exists
grep_r  4838/s     --   -81%   -83%   -90%
grep_e 25923/s   436%     --   -10%   -49%
hash   28848/s   496%    11%     --   -43%
exists 50524/s   944%    95%    75%     --


So in my test exists() is definitely faster.


John -- use Perl; program fulfillment

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Reply via email to