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)


In reply to Benchmark array passing Question by rakshas

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.