C++:使用 std::uint_fastn_t 的缺点
C++: Disadvatages of using std::uint_fastn_t
所以我偶然发现 When should I use the C++ fixed-width integer types and how do they impact performance? and Should I use cstdint?,其中列出了 <cstdint>
中定义的固定宽度整数类型的优点和缺点。
我有点喜欢将变量的预期范围编码成它的类型,但我也不想强制 CPU 做额外的操作只是为了使用 uint16_t
而不是一个普通的 int
当我不严格要求有一个变量正好保存 16 位时。
我还阅读了 std::uint_fast16_t
等类型。根据我的理解,使用这种类型应该确保我能够在该变量中存储一个 16 位数字,但我永远不必为使用这种类型支付任何运行时惩罚,因为在每个体系结构中,例如uint32_t
会更快,我会自动使用它。
这给我留下了一个问题:除了我确实需要一个精确位宽的变量之外,使用 std::uint_fast16_t
而不是说 unsigned int
有什么缺点吗?
编辑:这当然是假设内存消耗不是问题。如果是,我会用 std::uint_least16_t
代替。
are there any disadvantages of using std::uint_fast16_t
instead of say unsigned int
.
一个缺点:由于通常的促销活动,类型不确定。 uint16_fast_t
是否转换为 signed 或 unsigned?
uint16_fast_t fa = 1;
unsigned un = 1;
int i;
fa some_operator i --> may result in an `int` or `uint16_fast_t`
un some_operator i --> result is unsigned.
歧义可能会对更复杂的方程和溢出行为产生负面影响。
IMO,uint16_fast_t
仅在狭窄的受控代码中有用,不适用于一般性能改进。小心 Is premature optimization really the root of all evil?.
很多因素会影响这个结论,但通常为了性能,通常为了清晰起见最好输入unsigned
。
所以我偶然发现 When should I use the C++ fixed-width integer types and how do they impact performance? and Should I use cstdint?,其中列出了 <cstdint>
中定义的固定宽度整数类型的优点和缺点。
我有点喜欢将变量的预期范围编码成它的类型,但我也不想强制 CPU 做额外的操作只是为了使用 uint16_t
而不是一个普通的 int
当我不严格要求有一个变量正好保存 16 位时。
我还阅读了 std::uint_fast16_t
等类型。根据我的理解,使用这种类型应该确保我能够在该变量中存储一个 16 位数字,但我永远不必为使用这种类型支付任何运行时惩罚,因为在每个体系结构中,例如uint32_t
会更快,我会自动使用它。
这给我留下了一个问题:除了我确实需要一个精确位宽的变量之外,使用 std::uint_fast16_t
而不是说 unsigned int
有什么缺点吗?
编辑:这当然是假设内存消耗不是问题。如果是,我会用 std::uint_least16_t
代替。
are there any disadvantages of using
std::uint_fast16_t
instead of sayunsigned int
.
一个缺点:由于通常的促销活动,类型不确定。 uint16_fast_t
是否转换为 signed 或 unsigned?
uint16_fast_t fa = 1;
unsigned un = 1;
int i;
fa some_operator i --> may result in an `int` or `uint16_fast_t`
un some_operator i --> result is unsigned.
歧义可能会对更复杂的方程和溢出行为产生负面影响。
IMO,uint16_fast_t
仅在狭窄的受控代码中有用,不适用于一般性能改进。小心 Is premature optimization really the root of all evil?.
很多因素会影响这个结论,但通常为了性能,通常为了清晰起见最好输入unsigned
。