----- Original Message -----
From: "JupiterHost.Net" <[EMAIL PROTECTED]>
Date: Tuesday, January 25, 2005 11:01 am
Subject: Re: hash slice/exists vs. grep benchmark weirdness...

> > You have to stop spending so much time playing with all this bogus
> > benchmarking :)
> 
> It not bogus :) Its an example to find the best method for a project.
> 
> If you prefer I'll ask the question I was trying to answer with 
> the 
> benchmark:
> 
> Assuming you have an array of 0-15 elements is it quicker/more 
> efficient to:
> 
>  1) create a hash slice and use exists when checking for specific 
> onesor
>  2) grep a regex out of the array when checking for specific ones
> 
> >>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, first -- you're creating the hash inside the benchmark loop,
> > which is not particularly light-weight.
> 
> Good point,
> although in the real world app I'll have an array already so I 
> want to 
> see if its quicker to:
>  - make the hash via a slice and use exists
> or
>  - just grep the whole array each time with a regex
> If I use
>  my %hash = qw(1..20000);
> and exists
> vs.
>  my @array = qw(1..10000);
> IE - no slice
> 
> then exists is a bit faster 2% or so, the problem is I start with 
> an 
> array I have no control over.
> 
> So, I'd have to do a slice to get the hash (or loop through it or 
> otherwise do something to get the hash)
> 
> > Second:  You're using pathologically small lists.  Try running 
> it with
> > 10,000 elements instead of ten.
> 
> that makes them about even, with slice being 1% faster at times, 
> however 
> the size of the array in the real world app are similar to the 
> original 
> benchmark
> 
> > Third:  Premature optimization is a terrible thing.
> 
> Premature? Could you elaborate?

Just as an FYI, you don't need "exists" in your code at all. It is just a waste 
of time in your example. Should be beter writen as:

print "hi" if $n{11};

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


-- 
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