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)
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|