问题描述
我有一个非常简单的枚举,如下所示:
I have a very simple Enum as follows:
公共枚举颜色{
红色,蓝色,绿色;
}
在这里我放了三种颜色,但可能有 undefined
Here I've put three colours, but it may have a undefined size.
此枚举将用于可能具有 undefined 实例数量(数百,数千甚至数百万)的类中
This Enum will be used in a class which can have an undefined number of instances (hundreds, thousands or even millions).
在此类中,我有一个必须返回随机颜色的方法。
In this class I have a method that must return a random Colour.
我有两个选择
private Colour[] colours;
public Datastructure() {
colours = Colour.values();
}
public Colour getRandomColour() {
return colours[rand.nextInt() % colours.length];
}
或者我可以继续调用Colour.values()而不是创建颜色列表
Or I can keep calling Colour.values() instead of creating the colours list.
public Colour getRandromColour() {
return Colour.values()[rand.nexInt() % Colour.values().length]
在第一个选项中,将创建一个额外的数组。请记住,此类可能有很多实例,因此它可能被视为浪费内存,并且也可能影响运行时间(实例化数组)。
In the first option, an extra array is created. Keep in mind that this class may have many instances, so it could be considered a waste of memory, and may have a impact on running time as well (instantiating the array). Especially when there are a lot of class instances.
在第二个选项中,Colour.values()被调用了几次(在这个简单的示例中,它仅被调用了几次)时间,但是在我的项目中,它有点复杂,调用次数也更多),所以这可以被认为是对CPU使用率的浪费。
In the second option, Colour.values() is called a few times (in this simple example it is only a few times but in my project it's bit more complex and has more calls) so this can be considered a waste of CPU usage.
我更喜欢使用第二种方法,但是我我对Colour.values()方法的复杂性感到好奇,我担心它可能是线性O(n)。 n是枚举中颜色的数量。当颜色很多时,这将是可怕的。
I would prefer using the second option, but I'm curious about the complexity of the Colour.values() method, which I fear may be linear O(n). n being the number of Colours in the enum. This would be horrible when there are a lot of colours.
还是权衡利弊,选择两种邪恶中的最佳方法?
Or would it just be a weigh off and choose the best of two evils?
tl; dr Enum.values()的复杂性是什么?
tl;dr What is the complexity of Enum.values()?
推荐答案
Colour.values()
返回的数组始终包含相同的元素,但是 values()
创建每次调用 new 数组(所以它是O(n),其中n是枚举值的数量)。而且您永远不会修改该数组。因此,每次需要时调用该方法都是浪费时间。并且在DataStructure类的每个实例中存储数组都是浪费时间和内存。我只需调用该方法一次,然后将该数组缓存为一个常量:
The array returned by Colour.values()
always contains the same elements, but values()
creates a new array every time it's called (so it's O(n) where n is the number of enum values). And you never modify that array. So calling the method every time you need it is a waste of time. And storing an array in every instance of your DataStructure class is a waste of time and memory. I would simply call the method once and cache that array in a constant:
private static final Colour[] COLOURS = colour.values();
public Colour getRandomColour() {
return COLOURS[rand.nextInt(COLOURS.length)];
}
只需确保此数组永远不会暴露在课堂之外。
Just make sure this array is never exposed outside of the class.
还要注意 Random.nextInt(limit)
的用法,它确实可以满足您的要求,可能更快,并且表示意向比使用模更清楚。
Also note the usage of Random.nextInt(limit)
which does exactly what you want, is probably faster, and expressed the intent more clearly than using a modulo.
这篇关于enum.values()的复杂度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!