The answer points to @Riaan's comment on the vs constant and enumeration of performance, and it does not directly answer the OP question, so it can be considered a hindrance, I suppose. However, I find it important to understand how internal work is going on.
I removed the sample from his example and improved it to remove garbage collection and create a string that takes up more than 90% of the execution time. A warm-up phase has been added to ensure that the access point does compile methods.
There is one more thing, the standard is really a test on sites. The optimization for callites is very different for 1, for 2 for a few more and for more or more. A call is a call to an abstract (or just overridden) method.
Below is a test with 6 enumeration constants:
package t1; public class ZEnums { public enum MyEnum { A { boolean getBooleanValue(){ return true; }}, B { boolean getBooleanValue(){ return true; }}, C { boolean getBooleanValue(){ return false; }}, D { boolean getBooleanValue(){ return false; }}, E { boolean getBooleanValue(){ return false; }}, F { boolean getBooleanValue(){ return false; }}, ; abstract boolean getBooleanValue(); } public enum MyEnumAlt { A (true), B (true), C (false), D (false), E (false), F (false), ; private final boolean isTrue; MyEnumAlt( boolean isTrue){ this.isTrue = isTrue; } boolean getBooleanValue(){ return isTrue; }; } public static void main(String[] args) { log("Warming up...");
21: 08: 46.685 Warming up ...
148 1% t1.ZEnums :: testEnum @ 7 (125 bytes)
150 1 t1.ZEnums $ MyEnum $ 6 :: getBooleanValue (2 bytes)
152 2 t1.ZEnums $ MyEnum $ 1 :: getBooleanValue (2 bytes)
154 3 t1.ZEnums $ MyEnum $ 2 :: getBooleanValue (2 bytes)
155 4 t1.ZEnums $ MyEnum $ 3 :: getBooleanValue (2 bytes)
158 2% t1.ZEnums :: testAlt @ 7 (125 bytes)
162 5 t1.ZEnums :: testEnum (125 bytes)
164 6 t1.ZEnums :: testAlt (125 bytes)
21: 08: 46.716 Warm up: 1600000
21: 08: 46.716 Testing 40,000,000 iterations
21: 08: 46.716 ====
21: 08: 46.716 Testing with Overridden method ...
21: 08: 47.513 Overridden method version took 781ms , length: 160000000
21: 08: 47.513 Testing with Constant in c-tor ...
21: 08: 48.138 Constant in c-tor version took 625ms , length: 160000000
The code was run with the parameters w / -server -XX:+PrintCompilation . Of course, the difference is small. However, this is not an interesting problem. If you test the version with 2 enum constants, the result can be significantly different. For 2 call sites, the compiler generates code by setting the appropriate method. In the above test, which will remove the entire booleanValue call and may even execute the test in O (1).
The funniest part, however, comes from 2 to 3 enum constants, when the compiler starts using the built-in caches, and then the WOW constant and magic all change.
<h / "> The bottom line is that the correct test is really complex and includes some knowledge about how to compile JIT, when the GC can be a problem (either remove it or hug it), and so on.
References: