英文:
Are the fast integer types faster when stored in the CPU registers?
问题
以下是翻译好的部分:
我一直在思考快速整数类型:
int_fast8_t
,int_fast16_t
,int_fast32_t
,int_fast64_t
,uint_fast8_t
,uint_fast16_t
,uint_fast32_t
,uint_fast64_t
在过去的几天里,我提出了一个关于为什么(如果是这样的话)这些类型比其他整数类型更快的问题:
现在我进一步思考的是,如果:
- 更快的类型在某个环境(实现/架构相关)上确实更快,因此该环境反映了这个问题的理想环境,
- 具有
register
类的对象确实存储在CPU的寄存器中(但通常在声明为register
类时不一定需要一直存储在那里),它们在相同的环境中更快, - CPU的寄存器能够容纳所需的整数值,
- 即使使用
register
存储类声明,快速整数类型是否会更快?
比如:
register int_fastY_t i;
(前提是上述条件成立。)
还是它们会互相干扰并降低性能?
英文:
I have been thinking about the fast integer types:
int_fast8_t
, int_fast16_t
, int_fast32_t
, int_fast64_t
, uint_fast8_t
, uint_fast16_t
, uint_fast32_t
, uint_fast64_t
over the last days and I asked a question about why (if so) these types are faster than the other integer types:
Something I have further thought about now is, if:
- Faster types are faster on a certain environment (implementation/architecture-dependent) and thus that environment reflects an ideal environment for this question,
- Objects with the class of
register
are stored inside a register of the CPU (but in general do not always need to be stored there when declared with theregister
class), they are faster on the same environment, and - A register of the CPU is capable of holding the required integer value,
- Are the fast integer types even *faster if declared with the
register
storage class?
Like:
register int_fastY_t i;
*(Implying the premises above.)
Or do they interfere with each other and decrease the performance?
答案1
得分: 4
以下是您要翻译的内容:
"Everything is faster when stored in CPU registers."
"register
does not cause the compiler to store a value in a register. register
does absolutely nothing. Only extremely old compilers used register
to know which variables to store in registers. New compilers do it automatically. Even 20-year-old compilers do it automatically."
"It sounds like you are trying to make a program faster but you don't understand what the program is actually doing, so you are asking about all the speed-related things that you have ever heard about."
"I remind you that if you're on x86-64 using GCC, for example, then int_fast16_t
is the same as int64_t
, which is the same as int
and register
does absolutely nothing, so register int_fast16_t
is the same as int
. It's not a magic speed-up command."
"If you want to use these kinds of tricks to make your program faster, you should start by reading the assembly code that your compiler produces. Information for GCC or Visual C++. Then you can look for inefficient assembly code, and when you find some, you can find out how to speed it up. Right now, you are tilting at windmills."
英文:
Everything is faster when stored in CPU registers.
register
does not cause the compiler to store a value in a register. register
does absolutely nothing. Only extremely old compilers used register
to know which variables to store in registers. New compilers do it automatically. Even 20-year-old compilers do it automatically.
It sounds like you are trying to make a program faster but you don't understand what the program is actually doing, so you are asking about all the speed-related things that you have ever heard about.
I remind you that if you're on x86-64 using GCC, for example, then int_fast16_t
is the same as int64_t
, which is the same as int
and register
does absolutely nothing, so register int_fast16_t
is the same as int
. It's not a magic speed-up command.
If you want to use these kinds of tricks to make your program faster, you should start by reading the assembly code that your compiler produces. Information for GCC or Visual C++. Then you can look for inefficient assembly code, and when you find some, you can find out how to speed it up. Right now, you are tilting at windmills.
答案2
得分: 1
"我怕这里的答案是顾问的最爱:“取决于”。使用一个新关键字不会让底层的CPU更快 — 这是给编译器使用最快类型的提示。在许多体系结构中,将数字保存在寄存器中确实可以加快算术运算的速度,但这并不意味着在任何体系结构中都会更快,而且无论如何,“register”关键字都是对编译器的另一个提示 — 特别是在优化之后,它不保证生成的代码会有任何约束。"
英文:
I'm afraid that the answer here is a consultant's favorite: "it depends". Using a new keyword won't make the underlying CPU any faster — that's a hint to the compiler to use the fastest type. In many architectures, keeping numbers in registers does make arithmetic faster, but that doesn't mean it will be faster in any architecture, and in any case, the register
keyword is another hint to the compiler — it doesn't guarantee any constraints on the generated code, especially after optimization.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论