Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's a good example of register variable usage in C?

People also ask

What is register variable in C with example?

Register variables tell the compiler to store the variable in CPU register instead of memory. Frequently used variables are kept in registers and they have faster accessibility. We can never get the addresses of these variables. “register” keyword is used to declare the register variables.

What are register variables What are their advantages?

Advantages of Register variable: - Access optimization and speed of program execution: The operations of these variables are faster by orders of magnitude. - It is useful when you want to refer a variable frequently. - It allocates fast memory in the form of a register. - It helps to speed up program execution.

When should the register modifier be used?

When should the register modifier be used? Does it really help? The register modifier hints to the compiler that the variable will be heavily used and should be kept in the CPU's registers, if possible, so that it can be accessed faster.

What is the use of register keyword?

Register keyword tells compiler to store the particular variable in CPU registers so that it could be accessible fast. From a programmer's point of view register keyword is used for the variables which are heavily used in a program, so that compiler can speedup the code.


There is no good example of register usage when using modern compilers (read: last 15+ years) because it almost never does any good and can do some bad. When you use register, you are telling the compiler "I know how to optimize my code better than you do" which is almost never the case. One of three things can happen when you use 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 code when you use register, there is no reason to believe another will do the same. If you have some critical code that the compiler is not optimizing well enough your best bet is probably to use assembler for that part anyway but of course do the appropriate profiling to verify the generated code is really a problem first.


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

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


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.


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

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


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