Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Benchmark array passing Question

by rakshas (Novice)
on Jun 04, 2012 at 16:42 UTC ( #974325=perlquestion: print w/ replies, xml ) Need Help??
rakshas has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I have written a binary search program. It runs fine, and I want to see the Bench marking on it. Below is the code. I am sending the an array an the value to the binarysearch function which is supposed to calculate the search. However, the values are not going to the function. Please advice me

use warnings; use strict; use bytes; use Benchmark; #**************************Define the variables*********************** +************* my @binarysearcharray=('1','2','3','4','5'); my $binarysearchkey=4; sub binarysearch{ my ($array,$key)=@_; my @array=@$array; my ($low,$high) = (0,scalar(@$array)); # computes the length of the a +rray and gives it to $high while($low <= $high){ my $mid= int(($low+$high)/2); print "The New mid is : $mid\n"; if($array[$mid] < $key){ $low = $mid + 1; print " Calculating Lower Bound\n"; } elsif($array[$mid] == $key) { print "Match found at position $array[$mid]\n"; last; } else #($array[$high] > $key) { $high=$mid-1; print "Calculating Upper Bound\n"; } } }
####################Bench Marking Area########################### timethese(4,{binarysearch => 'binarysearch(\@binarysearcharray,2)'});

Output

Benchmark: timing 4 iterations of binarysearch... The New mid is : 0 Calculating Lower Bound The New mid is : 0 Calculating Lower Bound The New mid is : 0 Calculating Lower Bound The New mid is : 0 Calculating Lower Bound binarysearch: 0 wallclock secs ( 0.00 usr + 0.00 sys = 0.00 CPU) (warning: too few iterations for a reliable count)

Comment on Benchmark array passing Question
Download Code
Re: Benchmark array passing Question
by kennethk (Monsignor) on Jun 04, 2012 at 16:55 UTC
    You have a scope issue. You can see this if you change your array declaration from my @binarysearcharray=... to our @binarysearcharray=.... The eval for the benchmark occurs in Benchmark package, and variables declared with my are not visible outside your current scope, in contrast to our. For a little more description of scope, see Private Variables via my().

    Please note that if you include output in a benchmarking run, the i/o is likely to invalidate your conclusions.

    #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

      Works Like a Charm now!!!! Thanks!!!!!

Re: Benchmark array passing Question
by SuicideJunkie (Priest) on Jun 04, 2012 at 17:03 UTC

    If you're benchmarking, one comment is that you're wasting time and memory making a copy of the input array. You can drop my @array=@$array; and then change your $array[...'s into $array->[...'s

    Also, your brackets aren't lining up right. I don't think there are any styles that would allow for:

    } }

Re: Benchmark array passing Question
by jwkrahn (Monsignor) on Jun 04, 2012 at 22:27 UTC
    my ($low,$high) = (0,scalar(@$array)); # computes the length of the a +rray and gives it to $high

    You have an off-by-one error.    The value scalar(@$array) is not a valid index to any of the values in @$array.

    my ($low,$high) = (0,$#$array); # computes the length of the array an +d gives it to $high

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://974325]
Approved by kennethk
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (9)
As of 2014-08-28 05:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (257 votes), past polls