vendredi 2 octobre 2015

Comparison of enums values() method and class.getEnumConstants()

I'm trying to compare this two ways to achieving the enums values (with and without reflection).

So this is my test class:

public class ReflectionOnEnumsTests2 {

    enum TestEnum { ONE, TWO, THREE; }

    public static void main(String[] args) {
        long n = 600_000_000;
        int stub;

        //test without Reflection
        long timeStartWithoutReflection = System.currentTimeMillis();
        for (int i = 0; i < n; i++){
            TestEnum[] values = TestEnum.values();
            stub = values.length;
        }
        System.out.println("Time consuming with reflection: " + (System.currentTimeMillis() - timeStartWithoutReflection));

        //test Reflection
        long timeStartWithReflection = System.currentTimeMillis();
        for (int i = 0; i < n; i++){
            TestEnum[] values = TestEnum.class.getEnumConstants();
            stub = values.length;
        }
        System.out.println("Time consuming with reflection: " + (System.currentTimeMillis() - timeStartWithReflection));
    }
}

And I'm confused about the test results. There is approximately the same time consuming. I expected that class.getEnumConstants would be much slower than values() method.

Results:
Time consuming with reflection: 6050
Time consuming with reflection: 7483

JDK version: 1.8.0_60

Question:
So why there is no difference in performance?





Aucun commentaire:

Enregistrer un commentaire