问题
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:
Why are the fast integer types 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
registerare stored inside a register of the CPU (but in general do not always need to be stored there when declared with theregisterclass), 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
registerstorage class?
- Are the fast integer types even *faster if declared with the
Like:
register int_fastY_t i;
*(Implying the premises above.)
Or do they interfere with each other and decrease the performance?
回答1:
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:
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.
来源:https://stackoverflow.com/questions/59615901/are-the-fast-integer-types-faster-when-stored-in-the-cpu-registers