3

I have this simple code. If I change int to byte in MyClass then it works 1.5 slower for some reason. Any idea, why?

public class Test {

public static void main(String[] args) {
    double start = System.currentTimeMillis();
    MyClass[] arr = new MyClass[10_000_000];

    for (int i = 0; i < arr.length; i++) {
        arr[i] = new MyClass();
    }

    double end = System.currentTimeMillis();
    System.out.println(end-start);
    }
}
class MyClass {
    final public int pole = 50;
    int eshePole;
}
MaxNevermind
  • 2,784
  • 2
  • 25
  • 31

1 Answers1

10

It's difficult to trust the results from a test like this, as you haven't done any warmup to allow the JVM to do things like optimise code paths. It's worth looking at articles like:

https://wikis.oracle.com/display/HotSpotInternals/MicroBenchmarks

Your test is also incorrect as System.currentTimeMillis() returns a long, not a double.

Trisha
  • 3,891
  • 1
  • 25
  • 39