Re: faster solution
- From: Barry Schwarz <schwarzb@xxxxxxxx>
- Date: Tue, 07 Apr 2009 18:59:09 -0700
On Mon, 6 Apr 2009 11:12:22 -0700 (PDT), kizzienova@xxxxxxxxx wrote:
Dear all,
The following piece of code (a search algorithm) has to be evaluated
for more than 5e9 times in my program. Therefore, I'd like to obtain
te fastest implementationpossible. Can anyone help me improving this
piece of code?
Thanks,
Kizzie
int search (int ascnd, int ju, int jl, double xi, double *xpp, int P)
{
int jm, j;
while (ju-jl > 1)
{ /* if we are not yet done */
jm = (ju+jl) >> 1; /* compute a midpoint */
if (xi >= xpp[jm] == ascnd) {jl = jm; /* and replace
either the lower limit */ }
else {ju = jm; /* or the upper limit, as appropriate
*/ }
} /* repeat until the test condition is satisfied. */
if (xi == xpp[1]) j = 1;
xi and xpp[1] are independent of the while loop above.
else if (xi == xpp[P]) j = P-1;
xi and xpp[P] are also independent of the while loop.
else j = jl;
return j;
}
You can avoid the while loop completely for the cases where either of
the two above if statements evaluate to true.
Try something along the lines of
int search(...){...
if (xi == xpp[1]) return 1;
if (xi == xpp[P]) return P-1;
while (...)
{your while code}
return jl;
--
Remove del for email
.
- References:
- faster solution
- From: kizzienova
- faster solution
- Prev by Date: Re: Strange - a simple assignment statement shows error in VC++ but works in gcc !
- Next by Date: Re: Removing a file in C
- Previous by thread: Re: faster solution
- Next by thread: Request for code comment
- Index(es):
Relevant Pages
|