Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is there a performance hit when using enum.values() vs. String arrays?

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 => enum conversion - 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

like image 405
Asaf Avatar asked Mar 15 '10 09:03

Asaf


People also ask

Are enums faster than strings?

Comparisons of enums usually are faster than comparisons of strings. Enumerators have the size of the underlying integral type, while strings can be many characters long.

Are enums better than strings?

They are type safe and comparing them is faster than comparing Strings. Show activity on this post. If your set of parameters is limited and known at compile time, use enum . If your set of parameters is open and unkown at compile time, use strings.

Should I use enum or array?

The main difference is that an array is a value and an enum is a type. And One main difference we can say that an array is a collection of other values (that is it contains other values, you can iterate through them or access individual ones by index), whereas an enum value is simply one atomic value.

How efficient are enums?

Enums add at least 2x more bytes to the total APK size than plain constants and can use approximately 5-10xmore RAM than equivalent constants. So if you'd like your app to be memory- and performance-efficient, try to avoid enums in your code.


1 Answers

For enums, in order to maintain immutability, they clone the backing array every time you call the Values() method. This means that it will have a performance impact. How much depends on your specific scenario.

I have been monitoring my own Android app and found out that this simple call used 13.4% CPU time! in my specific case.

In order to avoid cloning the values array, I decided to simple cache the values as a private field and then loop through those values whenever needed:

private final static Protocol[] values = Protocol.values(); 

After this small optimisation my method call only hogged a negligible 0.0% CPU time

In my use case, this was a welcome optimisation, however, it is important to note that using this approach is a tradeoff of mutability of your enum. Who knows what people might put into your values array once you give them a reference to it!?

like image 195
Simon Langhoff Avatar answered Sep 30 '22 21:09

Simon Langhoff