C++ 什么是 uint_fast32_t,为什么要使用它而不是常规的 int 和 uint32_t?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8500677/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is uint_fast32_t and why should it be used instead of the regular int and uint32_t?
提问by Amumu
So the reason for typedef
:ed primitive data types is to abstract the low-level representation and make it easier to comprehend (uint64_t
instead of long long
type, which is 8 bytes).
所以typedef
:ed 原始数据类型的原因是抽象低级表示并使其更容易理解(uint64_t
而不是long long
类型,它是 8 个字节)。
However, there is uint_fast32_t
which has the same typedef
as uint32_t
. Will using the "fast" version make the program faster?
但是,有uint_fast32_t
与 相同typedef
的uint32_t
。使用“快速”版本会使程序更快吗?
回答by ybungalobill
int
may be as small as 16 bits on some platforms. It may not be sufficient for your application.uint32_t
is not guaranteed to exist. It's an optionaltypedef
that the implementation must provide iff it has an unsigned integer type of exactly 32-bits. Some have a 9-bit bytes for example, so they don't have auint32_t
.uint_fast32_t
states your intent clearly: it's a type of at least32 bits which is the best from a performance point-of-view.uint_fast32_t
may be in fact 64 bits long. It's up to the implementation.
int
在某些平台上可能小到 16 位。它可能不足以满足您的应用程序。uint32_t
不保证存在。typedef
如果它具有恰好 32 位的无符号整数类型,则实现必须提供它是可选的。例如,有些具有 9 位字节,因此它们没有uint32_t
.uint_fast32_t
清楚地说明您的意图:它是一种至少32 位的类型,从性能的角度来看是最好的。uint_fast32_t
实际上可能是 64 位长。这取决于实施。
... there is
uint_fast32_t
which has the same typedef asuint32_t
...
... 有
uint_fast32_t
与uint32_t
...相同的 typedef
What you are looking at is not the standard. It's a particular implementation (BlackBerry). So you can't deduce from there that uint_fast32_t
is always the same as uint32_t
.
你看的不是标准。这是一个特定的实现(黑莓)。所以你不能从那里推断出uint_fast32_t
总是与uint32_t
.
See also:
也可以看看:
回答by Nawaz
The difference lies in their exact-ness and availability.
不同之处在于它们的准确性和可用性。
The dochere says:
这里的文档说:
unsigned integer type with width of exactly8, 16, 32 and 64 bits respectively (provided only if the implementation directly supports the type):
uint8_t uint16_t uint32_t uint64_t
宽度分别为8、16、32和 64 位的无符号整数类型(仅当实现直接支持该类型时才提供):
uint8_t uint16_t uint32_t uint64_t
And
和
fastest unsigned unsigned integer type with width of at least8, 16, 32 and 64 bits respectively
uint_fast8_t uint_fast16_t uint_fast32_t uint_fast64_t
最快的无符号无符号整数类型,宽度分别至少为8、16、32 和 64 位
uint_fast8_t uint_fast16_t uint_fast32_t uint_fast64_t
So the difference is pretty much clear that uint32_t
is a type which has exactly32
bits, and an implementation should provide it only ifit has type with exactly32 bits, and then it can typedef that type as uint32_t
. This means, uint32_t
may or may not be available.
因此,区别非常明显,uint32_t
即具有精确32
位的类型,并且只有当它具有精确为32 位的类型时,实现才应提供它,然后它可以将该类型定义为uint32_t
. 这意味着,uint32_t
可能可用,也可能不可用。
On the other hand, uint_fast32_t
is a type which has at least32 bits, which also means, if an implementation may typedef uint32_t
as uint_fast32_t
ifit provides uint32_t
. If it doesn't provide uint32_t
, then uint_fast32_t
could be a typedef of any type which has at least 32
bits.
另一方面,uint_fast32_t
是一种至少有32 位的类型,这也意味着,如果一个实现可以 typedefuint32_t
就uint_fast32_t
好像它提供uint32_t
. 如果它不提供uint32_t
,则uint_fast32_t
可能是任何类型的 typedef,它至少具有32
位。
回答by Harley Sugarman
When you #include inttypes.h
in your program, you get access to a bunch of different ways for representing integers.
当你#include inttypes.h
在你的程序中时,你可以使用一系列不同的方式来表示整数。
The uint_fast*_t type simply defines the fastest type for representing a given number of bits.
uint_fast*_t 类型只是定义了表示给定位数的最快类型。
Think about it this way: you define a variable of type short
and use it several times in the program, which is totally valid. However, the system you're working on might work more quickly with values of type int
. By defining a variable as type uint_fast*t
, the computer simply chooses the most efficient representation that it can work with.
可以这样想:你定义了一个类型的变量,short
并在程序中多次使用它,这是完全有效的。但是,您正在使用的系统可能会更快地使用 type 值int
。通过将变量定义为 type uint_fast*t
,计算机只需选择它可以使用的最有效的表示。
If there is no difference between these representations, then the system chooses whichever one it wants, and uses it consistently throughout.
如果这些表示之间没有区别,则系统会选择它想要的任何一种,并始终一致地使用它。
回答by Gil Colgate
Note that the fast version could be larger than 32 bits. While the fast int will fit nicely in a register and be aligned and the like: but, it will use more memory. If you have large arrays of these your program will be slower due to more memory cache hits and bandwidth.
请注意,快速版本可能大于 32 位。虽然快速 int 将很好地适合寄存器并对齐等:但是,它将使用更多内存。如果你有大量的这些数组,你的程序会因为更多的内存缓存命中和带宽而变慢。
I don't think modern CPUS will benefit from fast_int32, since generally the sign extending of 32 to 64 bit can happen during the load instruction and the idea that there is a 'native' integer format that is faster is old fashioned.
我认为现代 CPU 不会从 fast_int32 中受益,因为通常在加载指令期间会发生 32 位到 64 位的符号扩展,并且存在更快的“本机”整数格式的想法是老式的。