[ACCEPTED]-What's a good example of register variable usage in C?-kr-c

Accepted answer
Score: 85

There is no good example of register usage 16 when using modern compilers (read: last 15 15+ years) because it almost never does 14 any good and can do some bad. When you 13 use register, you are telling the compiler 12 "I know how to optimize my code better than 11 you do" which is almost never the case. One 10 of three things can happen when you use 9 register:

  • The compiler ignores it, this is most likely. In this case the only harm is that you cannot take the address of the variable in the code.
  • The compiler honors your request and as a result the code runs slower.
  • The compiler honors your request and the code runs faster, this is the least likely scenario.

Even if one compiler produces better 8 code when you use register, there is no 7 reason to believe another will do the same. If 6 you have some critical code that the compiler 5 is not optimizing well enough your best 4 bet is probably to use assembler for that 3 part anyway but of course do the appropriate 2 profiling to verify the generated code is 1 really a problem first.

Score: 15

In general i agree with Robert, but as any good 5 rule this one has exceptions as well.
If 4 you working on deeply embedded system you 3 might know better than compiler how to optimize 2 the code for your specific application on your specific hardware architecture.

But in 99% of cases Roberts 1 explanation good for embedded word as well.

Score: 4

I know this is from quite some time, but 26 here is an implementation of a subprocedure 25 from heapsort in which the use of register 24 variables makes the algorithm faster, at 23 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;
        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 22 tested the algortihm with and without the 21 register keyword before the attributes and 20 executed it to sort a random array with 19 50,000,000 elements on my notebook, a few 18 times for each version.

the use of registers 17 dropped the heapsort time from ~135s to 16 ~125s.

I also tested with 5,000,000 elements 15 only, but executed it more times.

The version 14 without the register started at 11s but 13 each execution lowered the time until it 12 reached 9,65s and stopped at it

the version 11 with the register started at 10s and lowered 10 the time until 8,80s.

I think it has something 9 to do with the cache memory. Nonetheless 8 it seems the registers make the algorithm 7 faster by a constanct factor

Since these 6 variables are quite much used along the 5 algorithm, by ensuring they are on the register 4 instead of leaving this work to the compiler 3 led to a better result in this case. However, it 2 didn't improved the time that much.

Hopefully 1 thill will be helpful to somebody, greetings.

Score: 3

Another common case is when implementing 5 low-level interpreters. Keeping some state 4 in registers, eg. virtual machine stack 3 pointer, can reduce the memory access significantly 2 and speed up you code.

See vmgen — a generator of efficient virtual machine interpreters for an example 1 of the optimization (5.2 Top of stack caching).

Score: 1

first is, register variable should be use 10 for heavily used variables such as loop 9 control variable to enhance performance 8 by minimizing access time. secondary you 7 can use only and only register storage specifier 6 in this situation like , fun (auto 5 int a,auto int b) :error fun 4 (register int a,register int b) :right 3 only this would be run fun 2 (static int a,static int b) :error fun 1 (extern int a,extern int b) :error

Score: 0

Well This is a Question that needs multiple 10 answers because there are multiple coding 9 contexes: from the high level language perspective, the 8 mid level and low level (down to assembly) since 7 the C language can call assembly routines.

The 6 reason for using assembly instead of C is 5 exavtly because of performance issues encountered 4 during development soz yes there is a need 3 for the register keyword but no it is not 2 working as intended by developer in many 1 cases

More Related questions