我应该在 64 位架构中使用 Int32 代替 Int 或 Int64 来表示小数字吗
Should I use Int32 for small number instead of Int or Int64 in 64 bit architecture
我正在开发的 iOS 应用仅支持 64 位设备。
在 swift 中创建新的 Int
类型并考虑到我要存储的范围永远不会溢出 Int32
的事实,我想知道使用 Int32
是否有任何好处相反或 Int
/Int64
.
不,使用 Int。 Swift Programming Language 对此非常明确:
Unless you need to work with a specific size of integer, always use Int for integer values in your code. This aids code consistency and interoperability. Even on 32-bit platforms, Int can store any value between -2,147,483,648 and 2,147,483,647, and is large enough for many integer ranges.
"work with a specific size of integer," 文档描述了根据特定位宽定义的文件格式和网络协议等情况。即使你只数到 10,你仍然应该将它存储在 Int 中。
Int 类型不会自动转换,因此如果您有 Int32,并且函数需要 Int,则必须将其转换为 Int(x)
。这很快就会变得非常麻烦。为避免这种情况,Swift 强烈建议所有内容都为 Int,除非您有特殊理由不这样做。
你也应该避免使用 UInt,即使你的值是无符号的。当你的意思是 "this machine-word-sized bit pattern" 时你应该只使用 UInt 并且当你的意思是 "this bit-width bit pattern." 时你应该只使用大小的 UInts(UInt32 等)如果你的意思是 "a number" (即使是无符号数),你应该使用 Int.
Use UInt only when you specifically need an unsigned integer type with the same size as the platform’s native word size. If this isn’t the case, Int is preferred, even when the values to be stored are known to be nonnegative. A consistent use of Int for integer values aids code interoperability, avoids the need to convert between different number types, and matches integer type inference, as described in Type Safety and Type Inference.
有关进一步讨论性能的链接,请参阅下面 Peter 的评论。确实,在处理大型数据结构时,使用 32 位整数可以显着提高性能,尤其是因为缓存和局部性问题。但通常,这应该隐藏在管理额外复杂性的数据类型中,将性能关键代码与主系统隔离开来。如果您不小心,在 32 位和 64 位整数之间来回移动很容易压倒较小数据的优势。
因此,通常使用 Int。在某些情况下使用 Int32 有很多优势,但尝试将其用作默认值可能会损害性能,也可能会帮助提高性能,并且肯定会显着增加代码复杂性。
我正在开发的 iOS 应用仅支持 64 位设备。
在 swift 中创建新的 Int
类型并考虑到我要存储的范围永远不会溢出 Int32
的事实,我想知道使用 Int32
是否有任何好处相反或 Int
/Int64
.
不,使用 Int。 Swift Programming Language 对此非常明确:
Unless you need to work with a specific size of integer, always use Int for integer values in your code. This aids code consistency and interoperability. Even on 32-bit platforms, Int can store any value between -2,147,483,648 and 2,147,483,647, and is large enough for many integer ranges.
"work with a specific size of integer," 文档描述了根据特定位宽定义的文件格式和网络协议等情况。即使你只数到 10,你仍然应该将它存储在 Int 中。
Int 类型不会自动转换,因此如果您有 Int32,并且函数需要 Int,则必须将其转换为 Int(x)
。这很快就会变得非常麻烦。为避免这种情况,Swift 强烈建议所有内容都为 Int,除非您有特殊理由不这样做。
你也应该避免使用 UInt,即使你的值是无符号的。当你的意思是 "this machine-word-sized bit pattern" 时你应该只使用 UInt 并且当你的意思是 "this bit-width bit pattern." 时你应该只使用大小的 UInts(UInt32 等)如果你的意思是 "a number" (即使是无符号数),你应该使用 Int.
Use UInt only when you specifically need an unsigned integer type with the same size as the platform’s native word size. If this isn’t the case, Int is preferred, even when the values to be stored are known to be nonnegative. A consistent use of Int for integer values aids code interoperability, avoids the need to convert between different number types, and matches integer type inference, as described in Type Safety and Type Inference.
有关进一步讨论性能的链接,请参阅下面 Peter 的评论。确实,在处理大型数据结构时,使用 32 位整数可以显着提高性能,尤其是因为缓存和局部性问题。但通常,这应该隐藏在管理额外复杂性的数据类型中,将性能关键代码与主系统隔离开来。如果您不小心,在 32 位和 64 位整数之间来回移动很容易压倒较小数据的优势。
因此,通常使用 Int。在某些情况下使用 Int32 有很多优势,但尝试将其用作默认值可能会损害性能,也可能会帮助提高性能,并且肯定会显着增加代码复杂性。