简体   繁体   English

在 64 位架构中,我应该使用 Int32 而不是 Int 或 Int64

[英]Should I use Int32 for small number instead of Int or Int64 in 64 bit architecture

The iOS app I am working on supports only 64-bit devices.我正在开发的 iOS 应用程序仅支持 64 位设备。 When creating new Int type in swift and considering the fact that the range that I want to store will never overflow Int32 , I wonder if there is any benefit of using Int32 instead or Int / Int64 .在 swift 中创建新的Int类型并考虑到我要存储的范围永远不会溢出Int32的事实时,我想知道使用Int32Int / Int64是否有任何好处。

No, use Int.不,使用 Int。 The Swift Programming Language is quite explicit about this: Swift 编程语言对此非常明确:

Unless you need to work with a specific size of integer, always use Int for integer values in your code.除非您需要使用特定大小的整数,否则请始终在代码中使用 Int 作为整数值。 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.即使在 32 位平台上,Int 也可以存储 -2,147,483,648 和 2,147,483,647 之间的任何值,并且对于许多整数范围来说都足够大。

By "work with a specific size of integer," the documentation is describing situations such as file formats and networking protocols that are defined in terms of specific bit-widths.通过“使用特定大小的整数”,文档描述了根据特定位宽定义的文件格式和网络协议等情况。 Even if you're only counting to 10, you should still store it in an Int.即使你只数到 10,你仍然应该将它存储在一个 Int 中。

Int types do not automatically convert, so if you have an Int32, and a function requires an Int, you'd have to convert it as Int(x) . Int 类型不会自动转换,因此如果您有 Int32,并且函数需要 Int,则必须将其转换为Int(x) This gets very cumbersome very quickly.这很快就会变得非常麻烦。 To avoid that, Swift strongly recommends everything be an Int unless you have a specific reason to do otherwise.为避免这种情况,Swift 强烈建议所有内容都为 Int,除非您有特殊原因不这样做。

You should also avoid UInt, even if your value is unsigned.你也应该避免 UInt,即使你的值是无符号的。 You should only use UInt when you mean "this machine-word-sized bit pattern" and you should only use the sized UInts (UInt32, etc) when you mean "this bit-width bit pattern."当您指的是“这个机器字大小的位模式”时,您应该只使用 UInt,而当您指的是“这个位宽位模式”时,您应该只使用大小的 UInt(UInt32 等)。 If you mean "a number" (even an unsigned number), you should use Int.如果你的意思是“一个数字”(甚至是一个无符号数字),你应该使用 Int。

Use UInt only when you specifically need an unsigned integer type with the same size as the platform's native word size.仅当您特别需要与平台本机字大小相同大小的无符号整数类型时才使用 UInt。 If this isn't the case, Int is preferred, even when the values to be stored are known to be nonnegative.如果不是这种情况,则首选 Int,即使要存储的值已知为非负值也是如此。 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.对整数值一致使用 Int 有助于代码互操作性,避免在不同数字类型之间转换的需要,并匹配整数类型推断,如类型安全和类型推断中所述。


See Peter's comments below for some links to further discussion on performance.有关性能的进一步讨论的一些链接,请参阅下面彼得的评论。 It is very true that using 32-bit integers can be a significant performance improvement when working with large data structures, particularly because of caching and locality issues.在处理大型数据结构时,使用 32 位整数确实可以显着提高性能,特别是因为缓存和局部性问题。 But as a rule, this should be hidden within a data type that manages that extra complexity, isolating performance-critical code from the main system.但作为规则,这应该隐藏在管理额外复杂性的数据类型中,将性能关键代码与主系统隔离。 Shifting back and forth between 32- and 64-bit integers can easily overwhelm the advantages of smaller data if you're not careful.如果您不小心,在 32 位和 64 位整数之间来回移动很容易压倒较小数据的优势。

So as a rule, use Int.因此,通常使用 Int。 There are advantages to using Int32 in some cases, but trying to use it as a default is as likely to hurt performance as help it, and will definitely increase code complexity dramatically.在某些情况下使用 Int32 是有好处的,但是尝试将其用作默认值可能会损害性能和帮助它,并且肯定会显着增加代码复杂性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM