Why is unsafe code below compared to safe access to array not much faster? What slows down and ideas how to write it better? I'll try to edit Heapsort as unsafe.
public unsafe static void HeapSortU(double[] array, int low, int high)
{
int length = high - low + 1;
fixed (double* arrPtr = array, start = &array[low], stred = &array[low + length / 2 - 1], root = &array[high])
{
for (double* i = stred; i >= start; i--)
HeapifyU(start, root, i);
for (double* i = root; i >= start; i--)
{
double temp = *start;
*start = *i;
*i = temp;
HeapifyU(start, i, start);
}
}
}
private unsafe static void HeapifyU(double* start, double* root, double* bottom)
{
double* l, largest;
while (true)
{
largest = bottom; // initialize largest as root
l = bottom + (bottom - start) + 1; // left = 2*bottom + 1
if (l < root && *l > *largest)
largest = l;
l++; // right = 2*bottom + 2
if (l < root && *l > *largest)
largest = l;
if (largest != bottom)
{
double temp = *bottom;
*bottom = *largest;
*largest = temp;
bottom = largest;
}
else
break;
}
}
(I don't know what else to write, but the automatic check still reports: "It looks like your post is mostly code; please add some more details.")