enum vs android @Intdef - 哪个优化得更好
enum vs android @Intdef - which one is better optimized
我知道将常量与枚举进行比较时,常量占用更少 space 并且可以是原始的。我正在研究 android 中的 @Intdef annotation,有人可以告诉我使用 @Intdef 还是使用枚举更好的存储方式。 android 现在是否建议将 enum 放在一边,并在可能的情况下使用 @intdef 向前推进? @Intdef 可以做多态吗,我怀疑?
来自 android 关于内存的文档 overhead:
Enums often require more than twice as much memory as static constants. You should strictly avoid using enums on Android.
@Intdef 显然效率更高,它比仅具有静态最终整数的权重为零,它都是编译时指令。枚举是 类 并且正如您在 link 中提到的那样有足迹。 @Intdef 为您提供枚举的最基本功能,即值验证和 none 枚举的其他功能,例如自动字符串转换。
许多 android 文档都已过时,这很可能就是其中之一。回到 android 的早期,每一位都很重要,但现在设备的能力要强得多。就我个人而言,我会根据设计要求在这两个选项之间进行选择,而不会过于追求效率。此外,其中一些更高级注释的语法并不能使代码清晰易读,因此不喜欢它。但是,如果情况需要良好的旧静态整数,@Intdef 会以视觉混乱为代价为您提供一些保护。
除了之前的答案,我还要补充一点,如果您正在使用 Proguard(并且您绝对应该这样做以减小大小并混淆您的代码),那么您的 Enums
将自动转换为 @IntDef
只要有可能:
https://www.guardsquare.com/manual/configuration/optimizations
class/unboxing/enum
Simplifies enum types to integer constants, whenever possible.
因此,如果您有一些离散值,并且某些方法应该只允许采用这些值而不是其他相同类型的值,那么我会使用 Enum
,因为 Proguard 将使这个手动优化工作代码给我。
还有 here is 关于使用 Jake Wharton 的枚举的好post,请看一看。
As a library developer, I recognize these small optimizations that should be done as we want to have as little impact on the consuming app's size, memory, and performance as possible. But it's important to realize that throwing away an Iterator allocation vs. an indexed loop, using a HashMap vs. a binary-searched collection like SparseArray, and putting an enum in your public API vs. integer values where appropriate is perfectly fine. Knowing the difference to make informed decisions is what's important and the video nearly nails that except for this one stupid stat.
我知道将常量与枚举进行比较时,常量占用更少 space 并且可以是原始的。我正在研究 android 中的 @Intdef annotation,有人可以告诉我使用 @Intdef 还是使用枚举更好的存储方式。 android 现在是否建议将 enum 放在一边,并在可能的情况下使用 @intdef 向前推进? @Intdef 可以做多态吗,我怀疑?
来自 android 关于内存的文档 overhead:
Enums often require more than twice as much memory as static constants. You should strictly avoid using enums on Android.
@Intdef 显然效率更高,它比仅具有静态最终整数的权重为零,它都是编译时指令。枚举是 类 并且正如您在 link 中提到的那样有足迹。 @Intdef 为您提供枚举的最基本功能,即值验证和 none 枚举的其他功能,例如自动字符串转换。
许多 android 文档都已过时,这很可能就是其中之一。回到 android 的早期,每一位都很重要,但现在设备的能力要强得多。就我个人而言,我会根据设计要求在这两个选项之间进行选择,而不会过于追求效率。此外,其中一些更高级注释的语法并不能使代码清晰易读,因此不喜欢它。但是,如果情况需要良好的旧静态整数,@Intdef 会以视觉混乱为代价为您提供一些保护。
除了之前的答案,我还要补充一点,如果您正在使用 Proguard(并且您绝对应该这样做以减小大小并混淆您的代码),那么您的 Enums
将自动转换为 @IntDef
只要有可能:
https://www.guardsquare.com/manual/configuration/optimizations
class/unboxing/enum
Simplifies enum types to integer constants, whenever possible.
因此,如果您有一些离散值,并且某些方法应该只允许采用这些值而不是其他相同类型的值,那么我会使用 Enum
,因为 Proguard 将使这个手动优化工作代码给我。
还有 here is 关于使用 Jake Wharton 的枚举的好post,请看一看。
As a library developer, I recognize these small optimizations that should be done as we want to have as little impact on the consuming app's size, memory, and performance as possible. But it's important to realize that throwing away an Iterator allocation vs. an indexed loop, using a HashMap vs. a binary-searched collection like SparseArray, and putting an enum in your public API vs. integer values where appropriate is perfectly fine. Knowing the difference to make informed decisions is what's important and the video nearly nails that except for this one stupid stat.