开发者

when to use registers in C?

开发者 https://www.devze.com 2022-12-16 16:56 出处:网络
I have something like this register unsigned int a, b, c; int n; for (n = 0; n < 10; ++n){ c = a + b b = a

I have something like this

register unsigned int a, b, c;
int n;
for (n = 0; n < 10; ++n){
c = a + b
b = a
a = c
array[n] = c;
}

what it does, it doesn't matter. The code runs quickly the way开发者_Go百科 it is now, slower if the register keyword is removed. However, when I add in register before int n, it actually runs slower than now, but faster than if no registers is used.

Can someone explain this to me? Thanks.


How did you time this? In practice, register usually does nothing. It's a piece of cruft from when compiler technology was extremely primitive and compilers couldn't figure out register allocation themselves. It was supposed to be a hint to allocate a register to that variable and was useful for variables used very frequently. Nowadays, most compilers simply ignore it and allocate registers according to their own algorithms.


register gives the compiler a hint to place the variable in a register instead of memory/stack space. In some cases, there won't be enough registers for every variable you place this keyword on so placing it on too many variables can force some of the others out of registers again.

This is just a hint, though, and the compiler doesn't have to take it.


In gcc, register is definitely not ignored, unless you specify optimization options. Testing your code with something like this

unsigned int array[10];

int n;

#define REG register

int main()
{
    REG unsigned int a, b, c;

    for (n = 0; n < 10; ++n){
        c = a + b;
        b = a;
        a = c;
        array[n] = c;
    }
}

you obtain (depending on whether REG is defined or empty)

when to use registers in C?

http://picasaweb.google.com/lh/photo/v2hBpl6D-soIdBXUOmAeMw?feat=directlink

On the left is shown the result of using registers.


There are a limited number of registers available, so marking everything as register won't put everything in registers. Benchmarking is the only way to know if it's going to help or not. A good compiler should be able to figure out what variables to put into registers on its own, so you should probably benchmark some more before you decide that the register keywords helps.


The idea of using register is, your variable is used extremely often. If there is any operation with your variable, it will be copied to a register anyway. So counter (index variables) are candidates for this modifier. In the example of Diego Torres Milano from Jan 15 '10 at 1:57 I would make it this way:

unsigned int array[10];    

int main()
{
    register int n;
    unsigned int a = 1, b = 2, c;

    for (n = 0; n < 10; ++n){
        c = a + b;
        b = a;
        a = c;
        array[n] = c;
    }
}


There's a limit to allocatable registers. If you outgo it, you just end up with less efficient code.

My take is that if what you do is so important that you have to decide yourself what goes in a register and what doesn't, you should write it using assembly language.

For general purpose languages, I strongly believe that a compiler is better able to decide what goes in a register than a human. The proof being that while you're not sure how many variables you can put in registers, your compiler knows for sure.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号