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而不是长类型,即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具有与uint32_t相同的类型定义。使用“快速”版本会使程序更快吗?
4 个解决方案
#1
103
-
int
may be as small as 16 bits on some platforms. It may not be sufficient for your application. - 在某些平台上,int可能只有16位。这对你的申请来说可能是不够的。
-
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
. - uint32_t不能保证存在。它是一个可选的typedef,实现必须提供iff,它有一个32位的无符号整数类型。一些有9位字节,所以它们没有uint32_t。
-
uint_fast32_t
states your intent clearly: it's a type of at least 32 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. - 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:
参见:
-
Exotic architectures the standards committees care about.
标准委员会关心的奇异架构。
-
My opinion-based pragmatic view of integer types in C and C++.
我对C和c++中整数类型的基于观点的实用主义观点。
#2
28
The difference lies in their exact-ness and availability.
不同之处在于它们的准确和可用性。
The doc here says:
这里的医生说:
unsigned integer type with width of exactly 8, 16, 32 and 64 bits respectively (provided only if the implementation directly supports the type):
无符号整数类型,宽度分别为8、16、32和64位(仅当实现直接支持该类型时提供):
uint8_t uint16_t uint32_t uint64_t
And
和
fastest unsigned unsigned integer type with width of at least 8, 16, 32 and 64 bits respectively
最快的无符号无符号整数类型,宽度至少为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 exactly 32
bits, and an implementation should provide it only if it has type with exactly 32 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 least 32 bits, which also means, if an implementation may typedef uint32_t
as uint_fast32_t
if it 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位的类型,也就是说,如果一个实现可以将uint32_t定义为uint_fast32_t,如果它提供uint32_t。如果它不提供uint32_t,那么uint_fast32_t可以是至少有32位的任何类型的定义。
#3
2
When you #include inttypes.h
in your program, you get access to a bunch of different ways for representing integers.
当你# include inttypes。在你的程序中,你可以访问很多不同的表示整数的方法。
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的变量,并在程序中多次使用它,这是完全有效的。但是,您正在处理的系统可能会更快地使用int类型的值,通过将变量定义为uint_fast*t,计算机就可以选择最有效的表示方法。
If there is no difference between these representations, then the system chooses whichever one it wants, and uses it consistently throughout.
如果这些表示之间没有差异,那么系统选择它想要的任何一个,并始终使用它。
#4
0
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位的符号,而且“本机”整数格式更快的想法是过时的。
#1
103
-
int
may be as small as 16 bits on some platforms. It may not be sufficient for your application. - 在某些平台上,int可能只有16位。这对你的申请来说可能是不够的。
-
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
. - uint32_t不能保证存在。它是一个可选的typedef,实现必须提供iff,它有一个32位的无符号整数类型。一些有9位字节,所以它们没有uint32_t。
-
uint_fast32_t
states your intent clearly: it's a type of at least 32 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. - 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:
参见:
-
Exotic architectures the standards committees care about.
标准委员会关心的奇异架构。
-
My opinion-based pragmatic view of integer types in C and C++.
我对C和c++中整数类型的基于观点的实用主义观点。
#2
28
The difference lies in their exact-ness and availability.
不同之处在于它们的准确和可用性。
The doc here says:
这里的医生说:
unsigned integer type with width of exactly 8, 16, 32 and 64 bits respectively (provided only if the implementation directly supports the type):
无符号整数类型,宽度分别为8、16、32和64位(仅当实现直接支持该类型时提供):
uint8_t uint16_t uint32_t uint64_t
And
和
fastest unsigned unsigned integer type with width of at least 8, 16, 32 and 64 bits respectively
最快的无符号无符号整数类型,宽度至少为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 exactly 32
bits, and an implementation should provide it only if it has type with exactly 32 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 least 32 bits, which also means, if an implementation may typedef uint32_t
as uint_fast32_t
if it 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位的类型,也就是说,如果一个实现可以将uint32_t定义为uint_fast32_t,如果它提供uint32_t。如果它不提供uint32_t,那么uint_fast32_t可以是至少有32位的任何类型的定义。
#3
2
When you #include inttypes.h
in your program, you get access to a bunch of different ways for representing integers.
当你# include inttypes。在你的程序中,你可以访问很多不同的表示整数的方法。
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的变量,并在程序中多次使用它,这是完全有效的。但是,您正在处理的系统可能会更快地使用int类型的值,通过将变量定义为uint_fast*t,计算机就可以选择最有效的表示方法。
If there is no difference between these representations, then the system chooses whichever one it wants, and uses it consistently throughout.
如果这些表示之间没有差异,那么系统选择它想要的任何一个,并始终使用它。
#4
0
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位的符号,而且“本机”整数格式更快的想法是过时的。