什么时候使用C语言中的寄存器?

发布于 2024-08-17 13:21:52 字数 286 浏览 1 评论 0原文

我有这样的东西,

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

它的作用是什么,没关系。代码按照现在的方式运行得很快,如果删除 register 关键字,则运行速度会变慢。然而,当我在 int n 之前添加 register 时,它实际上运行得比现在慢,但比不使用寄存器时更快。

有人可以向我解释一下吗?谢谢。

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

爱的那么颓废 2024-08-24 13:21:52

你是怎么计时的?实际上,register 通常不执行任何操作。这是编译器技术极其原始并且编译器无法自行计算寄存器分配的时候的产物。它应该是为该变量分配寄存器的提示,并且对于经常使用的变量很有用。如今,大多数编译器只是忽略它并根据自己的算法分配寄存器。

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.

红墙和绿瓦 2024-08-24 13:21:52

register 向编译器提示将变量放置在寄存器而不是内存/堆栈空间中。在某些情况下,没有足够的寄存器来容纳您放置此关键字的每个变量,因此将其放置在太多变量上可能会再次迫使其他一些变量退出寄存器。

但这只是一个提示,编译器不必接受它。

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.

晨曦÷微暖 2024-08-24 13:21:52

在gcc中,寄存器绝对不会被忽略,除非你指定了优化选项。
使用您获得的类似内容测试您的代码

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;
    }
}

(取决于 REG 是否已定义或为空)

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

左边是使用寄存器的结果。

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)

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

On the left is shown the result of using registers.

甲如呢乙后呢 2024-08-24 13:21:52

可用的寄存器数量有限,因此将所有内容标记为寄存器不会将所有内容放入寄存器中。基准测试是了解它是否有帮助的唯一方法。一个好的编译器应该能够自行确定将哪些变量放入寄存器中,因此在确定寄存器关键字有帮助之前,您可能应该进行更多基准测试。

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.

南巷近海 2024-08-24 13:21:52

使用寄存器的想法是,你的变量被非常频繁地使用。如果对变量进行任何操作,无论如何它都会被复制到寄存器中。因此计数器(索引变量)是该修饰符的候选者。以 2010 年 1 月 15 日 1:57 的 Diego Torres Milano 为例,我会这样做:

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;
    }
}

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;
    }
}
感悟人生的甜 2024-08-24 13:21:52

可分配的寄存器是有限制的。如果你放弃它,你最终会得到效率较低的代码。

我的看法是,如果您所做的事情非常重要,以至于您必须自己决定什么进入寄存器、什么不进入寄存器,那么您应该使用汇编语言来编写它。

对于通用语言,我坚信编译器比人类能够更好地决定寄存器中的内容。证据是,虽然您不确定可以在寄存器中放入多少个变量,但您的编译器肯定知道。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文