I need a optimized binary search algorithm on a array of sorted numbers. I did this and found that using float to store numbers is faster than using integer, because at the end i must calculate
(frameNumber-this->frameNumber[imin])/(this->frameNumber[imax]-this->frameNumber[imin])
this->frameNumber[imin]
is the biggest frameNumber less equal that frameNumber
and this->frameNumber[imax]
is the smallest one greater equal than that. That code is to calculate the progress between that two keyframes.
the frameNumber array is static. I only have to sort it once. But access it many times with a binary search and the above code to calculate the progress.
The conversion from int to float spent some cycles. Then I discovered that in the asm there a a lot of fpu instructions. I'm worrying they might be slower than integer.
So here is the question. Can I convert an array of sorted floating point numbers to an int* and run a binary search on it?
That means:
void binary_search(float key,float* array,...)
{
int key_integer=*(int*)&key;
int* array_intege(int*)array;
binary_search_for_integers(key_integer,array_integer,...);
}
Or my above conclusion are wrong? (Such as casting int to float is not so costy, or comparision between floating points is the same fast as integers?
Thanks a lot!