I am trying to sort an array of doubles. It is extremely slow:
double price[1000];
void sortArrayOfDoubles(double price[ ], int length)
{
qsort(price, length, sizeof (double), compareDoubles);
}
I am using this as the compare method, which is very slow. Call it Method A:
int compareDoubles_SlowMethod(const void* elem1, const void* elem2)
{
if(*(const double*)elem1 < *(const double*)elem2)
return -1;
return *(const double*)elem1 > *(const double*)elem2;
}
So then I tried this one, Method B, which is more than 3x slower than the other one.
int compareDoubles_EvenSlowerMethod(const void *x, const void *y)
{
double xx = *(double*)x, yy = *(double*)y;
if (xx < yy) return -1;
if (xx > yy) return 1;
return 0;
}
Is there anything faster?
I would write it as:
This is essentially a mix of your two suggestions. Note that doing a callback just to compare a single value will never be optimally efficient. This is a drawback of how C's
qsort()
works; it would be best to inline the comparison inside the sort, but that's not possible in C.Unless, of course, you implement the sorting algorithm yourself and tailor it for
double
values.