Enum.values()和EnumSet.allOf()。哪一个更好? [英] Enum.values() vs EnumSet.allOf( ). Which one is more preferable?

查看:152
本文介绍了Enum.values()和EnumSet.allOf()。哪一个更好?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看着 EnumSet.allOf ,看起来效率非常高,特别是对于少于64个值的枚举。

I looked under the hood for EnumSet.allOf and it looks very efficient, especially for enums with less than 64 values.

基本上所有的集合共享所有可能的枚举值的单个数组,唯一的其他信息是一个位掩码,在 allOf 的情况下,

Basically all sets share the single array of all possible enum values and the only other piece of information is a bitmask which in case of allOf is set in one swoop.

另一方面,枚举值()似乎有点黑魔法。此外,它返回一个数组,而不是一个集合,所以在许多情况下,它必须用Arrays.asList()进行装饰,以便在任何可以收集的地方使用。

On the other hand Enum.values() seems to be a bit of black magic. Moreover it returns an array, not a collection, so in many cases it must be decorated with Arrays.asList( ) to be usable in any place that expects collection.

所以,应该 EnumSet.allOf 更适合 Enum.values

更具体地说,应该使用迭代器的哪种形式的

More specifically, which form of for iterator should be used:

for ( final MyEnum val: MyEnum.values( ) );

for ( final MyEnum val: EnumSet.allOf( MyEnum.class ) );


推荐答案

因为我没有收到我的问题的答案我已经决定对我自己做一些测试。

Because I did not receive the answer to my question on which one is more efficient, I've decided to do some testing of my own.

我已经测试了 values() Arrays.asList(values()) EnumSet.allOf()
对于不同的枚举大小,我重复了这些测试10,000,000次。以下是测试结果:

I've tested iteration over values(), Arrays.asList( values() ) and EnumSet.allOf( ). I've repeated these tests 10,000,000 times for different enum sizes. Here are the test results:

oneValueEnum_testValues         1.328
oneValueEnum_testList           1.687
oneValueEnum_testEnumSet        0.578

TwoValuesEnum_testValues        1.360
TwoValuesEnum_testList          1.906
TwoValuesEnum_testEnumSet       0.797

ThreeValuesEnum_testValues      1.343
ThreeValuesEnum_testList        2.141
ThreeValuesEnum_testEnumSet     1.000

FourValuesEnum_testValues       1.375
FourValuesEnum_testList         2.359
FourValuesEnum_testEnumSet      1.219

TenValuesEnum_testValues        1.453
TenValuesEnum_testList          3.531
TenValuesEnum_testEnumSet       2.485

TwentyValuesEnum_testValues     1.656
TwentyValuesEnum_testList       5.578
TwentyValuesEnum_testEnumSet    4.750

FortyValuesEnum_testValues      2.016
FortyValuesEnum_testList        9.703
FortyValuesEnum_testEnumSet     9.266

这些是从命令行运行的测试结果。当我从Eclipse中运行这些测试时,我得到了压倒性的支持 testValues 。基本上它小于 EnumSet 即使是小枚举。我相信,性能提升来自于($ val code)循环中的中的数组迭代器的优化。

These are results for tests ran from command line. When I ran these tests from Eclipse, I got overwhelming support for testValues. Basically it was smaller than EnumSet even for small enums. I believe that the performance gain comes from optimization of array iterator in for ( val : array ) loop.

另一方面,只要你需要一个java.util.Collection来传递, Arrays.asList()丢失到$ code> EnumSet.allOf ,特别是对于小枚举,我相信在任何给定的代码库中都是多数。

On the other hand, as soon as you need a java.util.Collection to pass around, Arrays.asList( ) looses over to EnumSet.allOf, especially for small enums, which I believe will be a majority in any given codebase.

所以,我会说你应该使用

So, I would say you should use

for ( final MyEnum val: MyEnum.values( ) )

Iterables.filter(
    EnumSet.allOf( MyEnum.class ),
    new Predicate< MyEnum >( ) {...}
)

并且只使用 Arrays.asList(MyEnum.values())其中 java.util.List 绝对需要。

And only use Arrays.asList( MyEnum.values( ) ) where java.util.List is absolutely required.

这篇关于Enum.values()和EnumSet.allOf()。哪一个更好?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆