Wow, 36500% faster. That's a worthwhile saving. Glad it helped. It's always good to use a geometric search rather than a linear one when you have any form of sorted data that you can use the split the dif algoritm on. The number of tries to find the desired position is given by:
print "Num items Geom avg Lin avg Lin:Geom\n";
for ( my $num_items = 2; $num_items < 2<<20; $num_items <<= 1 ) {
# geometric
my $geom_max = int(log($num_items)/log(2))+1;
my $geom_avg = int(log(($num_items/2))/log(2))+1;
# linear
my $lin_max = $num_items;
my $lin_avg = $num_items/2;
printf "%8d %8d %8d %8d\n",
$num_items, $geom_avg, $lin_avg, $lin_avg/$geom_avg;
}
Should wrap it in a module one day :-)
cheers
tachyon
s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print
-
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.
|