枚举与 int 标志?

发布于 2024-10-19 14:47:36 字数 66 浏览 6 评论 0 原文

只是想就此事发表一些意见。我一直使用 int 标志,并且只是好奇如果我在 Java 中使用枚举可能会带来易用性的性能?

Just wanted some opinions on the matter. I have always used int flags, and was just curious on possible performance of ease of use if I were to use enumerations in Java?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

雨落星ぅ辰 2024-10-26 14:47:36

我非常怀疑您是否会看到性能优势 - 在某些情况下甚至可能会出现性能损失,具体取决于您如何使用它们。但我怀疑它们是否重要。

好处不是在性能方面,而是在更清楚地表达您的意图方面,从而产生更具可读性(和类型安全)的代码。例如,对于整数标志,如果您尝试在方法调用中使用(例如)HTTP_STATUS_UNAUTHORIZED 作为文件共享模式的值,则不会有任何抱怨...而如果这两个都是枚举,则参数将被强类型化为 < em>真的只允许文件共享模式(或者空,诚然,假设您正在谈论Java)。

I very much doubt you'd see performance benefits - and in some cases there may even be performance penalties, depending on how you're using them. I doubt they'd be significant though.

The benefit isn't in terms of performance - it's in terms or expressing your intentions more clearly, leading to more readable (and type-safe) code. For example, with integer flags nothing's going to complain if you try to use (say) HTTP_STATUS_UNAUTHORIZED as a value for your file sharing mode in a method call... whereas if both of those were enums, the parameter would be strongly typed to really only allow file sharing modes (or null, admittedly, assuming you are talking about Java).

聚集的泪 2024-10-26 14:47:36

和往常一样,Effective Java 说得最好:

使用 int 枚举的程序
图案很脆。因为int
枚举是编译时常量,它们
被编译到使用的客户端中
他们。如果 int 与关联
枚举常量发生变化,其客户
必须重新编译。如果他们不是,
他们仍然会奔跑,但他们的
行为将是未定义的。


没有简单的方法来翻译 int 枚举
常量转换为可打印字符串。如果
你打印这样一个常数或显示
从调试器中,你所看到的只是
号,这不是很有帮助。
没有可靠的迭代方法
遍历 a 中的所有 int 枚举常量
组,甚至获得大小
一个 int 枚举组。

(+ 10 页以上解释了为什么枚举更好:-))

来源: 第 30 项:使用枚举代替 int 常量

As usual, Effective Java says it best:

Programs that use the int enum
pattern are brittle. Because int
enums are compile-time constants, they
are compiled into the clients that use
them. If the int associated with an
enum constant is changed, its clients
must be recompiled. If they aren’t,
they will still run, but their
behavior will be undefined.

There is
no easy way to translate int enum
constants into printable strings. If
you print such a constant or display
it from a debugger, all you see is a
number, which isn’t very helpful.
There is no reliable way to iterate
over all the int enum constants in a
group, or even to obtain the size of
an int enum group.

(+ 10 more pages of why enums are better :-))

Source: Item 30: Use enums instead of int constants

别想她 2024-10-26 14:47:36

枚举更清楚地表达您想要做什么。使用它们并且不用担心性能;性能影响可以忽略不计。

Enumerations more clearly express what you are trying to do. Use them and do not worry about performance; performance impact will be negligible.

趴在窗边数星星i 2024-10-26 14:47:36

使用位掩码标志的等效方法是使用 EnumSet(它恰好使用位掩码)

enum Colour { RED, GREEN, BLUE }
Set<Colour> colours = EnumSet.noneOf(Colour.class);
colours.add(Colour.RED);
colours.add(Colour.GREEN);
if (colours.contains(Colour.RED)) // true
   System.out.println(colours+" contains RED");

if (colours.contains(Colour.BLUE)) // false

An equivalent of using bit mask flags is to use EnumSet (which happens to use a bit mask)

enum Colour { RED, GREEN, BLUE }
Set<Colour> colours = EnumSet.noneOf(Colour.class);
colours.add(Colour.RED);
colours.add(Colour.GREEN);
if (colours.contains(Colour.RED)) // true
   System.out.println(colours+" contains RED");

if (colours.contains(Colour.BLUE)) // false
路还长,别太狂 2024-10-26 14:47:36

它们基本上是相同的,只是枚举是自我记录的。我建议一旦其他人可能接触到您的代码,就使用枚举。

They're basically the same thing except enumerations are self-documented. I recommend using enumerations as soon as someone else might be in contact with your code.

自此以后,行同陌路 2024-10-26 14:47:36

具有自己的方法和 Enum 类型“http://download.oracle.com/javase/6/docs/api/java/util/EnumSet.html”rel="nofollow">优化容器。你应该更喜欢它而不是 int 标志,因为它更安全并且可能更高效(尽管我怀疑它有很多东西!)。

An Enum a type with its own methods and optimized containers. You should prefer it over int flags because it's safer and possibly more efficient (though I doubt there's very much in it at all!).

长亭外,古道边 2024-10-26 14:47:36

除了类型安全代码之外,您还可以使用单引用来共享集合(可重用性)

In addition to the type-safe code, you have a single-reference to share the collection (re-usability)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文