I modified your code slightly, to show the speed differences.

Relevant NYTProf output:

Line State
ments
Time
on line
Calls Time
in subs
Code
37112µsfor (1..100000) {
38100000928ms1000004.23s $result=BinSearch(\@a, \$w);
# spent 4.23s making 100000 calls to main::BinSearch, avg 42µs/call
391000001.46s100000728ms say $result;
# spent 728ms making 100000 calls to main::CORE:say, avg 7µs/call
401000001.43s ($result)=grep { $a$_ eq $w } 0..$#a;
411000001.65s100000673ms say $result;
# spent 673ms making 100000 calls to main::CORE:say, avg 7µs/call
42}

Modified code:

my @a = qw(format type ascii hex pos len binary search perl unix eof a +rray word); @a = sort @a; my $w = "len"; say "@a"; my $result; open STDOUT,'>','/dev/null'; for (1..100000) { $result=BinSearch(\@a, \$w); say $result; ($result)=grep { $a[$_] eq $w } 0..$#a; say $result; }

Even with 10,000 pieces of junk in front of the searched element, grep still beats your BinSearch easily:

Relevant NYTProf output:

Line State
ments
Time
on line
Calls Time
in subs
Code
3811.66msfor (1..1000) {
39100024.1ms100014.4s $result=BinSearch(\@a, \$w);
# spent 14.4s making 1000 calls to main::BinSearch, avg 14.4ms/call
40100046.9ms100034.3ms say $result;
# spent 34.3ms making 1000 calls to main::CORE:say, avg 34µs/call
4110006.35s ($result)=grep { $a$_ eq $w } 0..$#a;
42100062.3ms100029.4ms say $result;
# spent 29.4ms making 1000 calls to main::CORE:say, avg 29µs/call

Modified code:

my @a = ('00junk') x 10000; push @a,qw(format type ascii hex pos len binary search perl unix eof a +rray word); @a = sort @a; my $w = "len"; say "@a"; my $result; open STDOUT,'>','/dev/null'; for (1..1000) { $result=BinSearch(\@a, \$w); say $result; ($result)=grep { $a[$_] eq $w } 0..$#a; say $result; }

Alexander

--
Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

In reply to Re: Binary search algorithm. by afoken
in thread Binary search algorithm. by kindlychung

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.