Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Performance of Java Enums?

Tags:

I am implementing a 2-player game that will be run in a tight loop literally hundreds of thousands of times, being then performance paramount.

My code actually looks something like this:

public class Table {     private final int WHITE_PLAYER = +1;     private final int BLACK_PLAYER = -1;      private final int currentPlayer;     private final int otherPlayer;      ... } 

I was wondering if I would get any performance hit would I choose to replace

private final int WHITE_PLAYER = +1; private final int BLACK_PLAYER = -1; 

to an enum defined as

public enum Players {     WhitePlayer,     BlackPlayer } 

I had the idea that enums were just syntactic sugar over integer constants, and taking a glaze look over the bytecode generated for a test enum, as well as the code calling it, seems to indicate that using them is indeed the same as making a static method call but for some enum infrastructure that is set up when it's first run.

Is my assumption that it is indeed the same to use enums as static constants correct or am I missing something here?

like image 859
devoured elysium Avatar asked Nov 06 '11 17:11

devoured elysium


People also ask

Are enums good Java?

When you need a predefined list of values which do represent some kind of numeric or textual data, you should use an enum. You should always use enums when a variable (especially a method parameter) can only take one out of a small set of possible values.

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.

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.

How much memory does an enum take in Java?

If the largest value in the enum is < 256 this will be one byte, two bytes if the largest value is < 65536, etc.


2 Answers

In a micro-benchmark, yes, checking integer constant equality will be faster than checking enum constant equality.

However, in a real application, let alone a game, this will be totally irrelevant. The things that are happening in the AWT subsystem (or any other GUI toolkit) dwarf these micro-performance considerations by many orders of magnitude.

EDIT

Let me elaborate a little then.

An enum comparison goes like this:

aload_0 getstatic if_acmpne 

An integer comparison for a small integer goes like this:

iload_0 iconst_1 if_icmpne 

Obviously, the first is more work than the second, although the difference is quite small.

Run the following test case:

class Test {      static final int ONE = 1;     static final int TWO = 2;      enum TestEnum {ONE, TWO}      public static void main(String[] args) {         testEnum();         testInteger();         time("enum", new Runnable() {             public void run() {                 testEnum();              }         });         time("integer", new Runnable() {             public void run() {                 testInteger();             }         });     }      private static void testEnum() {         TestEnum value = TestEnum.ONE;         for (int i = 0; i < 1000000000; i++) {             if (value == TestEnum.TWO) {                 System.err.println("impossible");             }         }     }      private static void testInteger() {         int value = ONE;         for (int i = 0; i < 1000000000; i++) {             if (value == TWO) {                 System.err.println("impossible");             }         }     }      private static void time(String name, Runnable runnable) {         long startTime = System.currentTimeMillis();         runnable.run();         System.err.println(name + ": " + (System.currentTimeMillis() - startTime) + " ms");     } } 

and you will find that the enum comparison is slower that the integer comparison, on my machine by around 1.5%.

All I was saying is that this difference will not matter in a real application ("Premature optimization is the root of all evil"). I deal with performance problems on a professional basis (see my profile) and I have never seen a hot spot that could be traced to something like this.

like image 167
Ingo Kegel Avatar answered Sep 19 '22 13:09

Ingo Kegel


You should care about having nice and readable code before you care about performance. Until your profiling results (no guessing!) show that enums are the bottleneck, forget about performance and use whatever is easier to understand.

like image 24
unbeli Avatar answered Sep 21 '22 13:09

unbeli