I know this is from quite some time, but here is an implementation of a subprocedure from heapsort in which the use of register variables makes the algorithm faster, at least using gcc 4.5.2 to compile the code
inline void max_heapify(int *H, int i){
char OK = FALSE;
register int l, r, max, hI;
while(!OK){
OK = TRUE;
l = left(i);
r = right(i);
max = i;
if(l <= H[SIZE] && H[l] > H[i]){
max = l;
}
if(r <= H[SIZE] && H[r] > H[max]){
max = r;
}
if(max != i){
OK = FALSE;
hI = H[i];
H[i] = H[max];
H[max] = hI;
i = max;
}
}
}
I tested the algortihm with and without the register keyword before the attributes and executed it to sort a random array with 50,000,000 elements on my notebook, a few times for each version.
the use of registers dropped the heapsort time from ~135s to ~125s.
I also tested with 5,000,000 elements only, but executed it more times.
The version without the register started at 11s but each execution lowered the time until it reached 9,65s and stopped at it
the version with the register started at 10s and lowered the time until 8,80s.
I think it has something to do with the cache memory. Nonetheless it seems the registers make the algorithm faster by a constanct factor
Since these variables are quite much used along the algorithm, by ensuring they are on the register instead of leaving this work to the compiler led to a better result in this case. However, it didn't improved the time that much.
Hopefully thill will be helpful to somebody, greetings.