rakshas has asked for the wisdom of the Perl Monks concerning the following question:
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
####################Bench Marking Area########################### timethese(4,{binarysearch => 'binarysearch(\@binarysearcharray,2)'});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"; } } }
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)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Benchmark array passing Question
by kennethk (Abbot) on Jun 04, 2012 at 16:55 UTC | |
by rakshas (Novice) on Jun 04, 2012 at 17:01 UTC | |
|
Re: Benchmark array passing Question
by SuicideJunkie (Vicar) on Jun 04, 2012 at 17:03 UTC | |
|
Re: Benchmark array passing Question
by jwkrahn (Abbot) on Jun 04, 2012 at 22:27 UTC |