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

眉间皱痕 提交于 2019-11-28 17:27:46

问题


I'm reading through K&R and came to the small section on register variables, and was wondering if people here have some good examples of this put into practice.

From section 4.7 in K&R:

The register declaration looks like
register int x;
register char c;

To be clear, I'm just hoping to see some cool code samples. I (am pretty sure that I) understand the subject matter so don't feel the need to type up a verbose explanation (unless you want to).


回答1:


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.




回答2:


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.




回答3:


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.




回答4:


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).




回答5:


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




回答6:


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

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



来源:https://stackoverflow.com/questions/314994/whats-a-good-example-of-register-variable-usage-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!