I’m using enumerations to replace String constants in my java app (JRE 1.5).
Is there a performance hit when I treat the enum as a static array of names in a method that is called constantly (e.g. when rendering the UI)?
My code looks a bit like this:
public String getValue(int col) {
return ColumnValues.values()[col].toString();
}
Clarifications:
- I’m concerned with a hidden cost related to enumerating
values()repeatedly (e.g. inside paint() methods). - I can now see that all my scenarios include some
int=>enumconversion – which is not Java’s way.
What is the actual price of extracting the values() array? Is it even an issue?
Android developers
Read Simon Langhoff’s answer below, which has pointed out earlier by Geeks On Hugs in the accepted answer’s comments. Enum.values() must do a defensive copy
Enum.values()gives you a reference to an array, and iterating over an array of enums costs the same as iterating over an array of strings. Meanwhile, comparing enum values to other enum values can actually be faster that comparing strings to strings.Meanwhile, if you’re worried about the cost of invoking the
values()method versus already having a reference to the array, don’t worry. Method invocation in Java is (now) blazingly fast, and any time it actually matters to performance, the method invocation will be inlined by the compiler anyway.So, seriously, don’t worry about it. Concentrate on code readability instead, and use
Enumso that the compiler will catch it if you ever try to use a constant value that your code wasn’t expecting to handle.If you’re curious about why enum comparisons might be faster than string comparisons, here are the details:
It depends on whether the strings have been interned or not. For
Enumobjects, there is always only one instance of each enum value in the system, and so each call toEnum.equals()can be done very quickly, just as if you were using the==operator instead of theequals()method. In fact, withEnumobjects, it’s safe to use==instead ofequals(), whereas that’s not safe to do with strings.For strings, if the strings have been interned, then the comparison is just as fast as with an
Enum. However, if the strings have not been interned, then theString.equals()method actually needs to walk the list of characters in both strings until either one of the strings ends or it discovers a character that is different between the two strings.But again, this likely doesn’t matter, even in Swing rendering code that must execute quickly. 🙂
@Ben Lings points out that
Enum.values()must do a defensive copy, since arrays are mutable and it’s possible you could replace a value in the array that is returned byEnum.values(). This means that you do have to consider the cost of that defensive copy. However, copying a single contiguous array is generally a fast operation, assuming that it is implemented “under the hood” using some kind of memory-copy call, rather than naively iterating over the elements in the array. So, I don’t think that changes the final answer here.