是什么在C寄存器变量使用的一个很好的例子? [英] What's a good example of register variable usage in C?

查看:161
本文介绍了是什么在C寄存器变量使用的一个很好的例子?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读通过K&安培; R,来到对寄存器变量的一小部分,并想知道如果人们在这里有这个放的一些很好的例子付诸实施

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.

这部分4.7 K&放大器; R:

From section 4.7 in K&R:

寄存器声明看起来像

       注册INT X;

       注册焦炭℃;

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

要清楚,我只是希望能看到一些很酷的code样品。我(时pretty确保I)了解标的物,所以不要觉得需要键入了一个详细的解释(除非你想)。

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

推荐答案

有是用现代的编译器时使用的寄存器没有很好的例子(阅读:过去15年以上),因为它几乎从来不会有什么好处,可以做一些不好的。当您使用注册,你告诉编译器:我知道如何优化我的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:


  • 编译器会忽略它,这是最有可能的。在这种情况下,唯一的坏处是,你不能在code中的变量的地址。

  • 编译器荣誉您的要求,因此在code运行速度较慢。

  • 编译器荣誉您的要求和code运行速度更快,这是最可能的情况。

即使有人编译器生成当您使用更好的注册code,有没有理由相信另一个将这样​​做。如果你有一些关键的code编译器不优化不够好,你最好的选择可能是使用汇编程序的那部分无论如何,当然,但做了相应的分析,以验证所生成的code是一个真正的问题第一。

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.

这篇关于是什么在C寄存器变量使用的一个很好的例子?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆