Java performance when creating objects

I am trying to improve my knowledge of Java performance optimization and I have tried several ways to create an object.
I've encountered a behavior that I'm not familiar with regarding the use of final members in a class: creating an object is much cheaper (in terms of time) if the members are not final. Is this correct or are there any plugins in my code?

An object with not the last members:

public class ComplexNumber {
    private double re, im;

    public ComplexNumber(double _re, double _im) {
        re = _re;
        im = _im;
    }

    public void setRe (double _re) {
        re = _re;
    }

    public void setIm (double _im) {
        im = _im;
    }

    @Override
    public String toString() {
        return re + " + i" + im;
    }

    @Override
    public int hashCode() {
        return 47 + 31*(int)re + 31*(int)im;
    }
}  

      

Object with end members:

public class FinalComplexNumber {
    private final double re, im;

    public FinalComplexNumber(double _re, double _im) {
        re = _re;
        im = _im;
    }

    @Override
    public String toString() {
        return re + " + i" + im;
    }

    @Override
    public int hashCode() {
        return 47 + 31*(int)re + 31*(int)im;
    }
}  

      

Main class

public class PerformanceTest {

    private static final long ITERATIONS = 100000000l;

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {


        ComplexNumber nr = new ComplexNumber(0, 0);
        System.out.println(nr);

        long time = System.currentTimeMillis();
        for (int i = 0; i < ITERATIONS; i++) {
            ComplexNumber num = new ComplexNumber(i, i);
        }
        System.out.println(System.currentTimeMillis() - time);

        time = System.currentTimeMillis();
        for (int i = 0; i < ITERATIONS; i++) {
            nr.setIm(i);
            nr.setRe(i);
        }
        System.out.println(System.currentTimeMillis() - time);

        time = System.currentTimeMillis();
        for (int i = 0; i < ITERATIONS; i++) {
            FinalComplexNumber num = new FinalComplexNumber(i, i);
        }
        System.out.println(System.currentTimeMillis() - time);
    }
}  

      

Results:

run:
0.0 + i0.0
953
219
7875
BUILD SUCCESSFUL (total time: 9 seconds)

      

+3


source to share


2 answers


object creation is much cheaper (in terms of time) if the members are not final.

No, that's not true at all. Basically, your approach to benchmarking is broken:



  • You are using System.currentTimeMillis()

    , which is usually a bad idea for benchmarking; System.nanoTime()

    is preferred for measuring elapsed time.
  • You don't provide a JIT workout
  • You are not using instantiated objects, which could allow JIT optimizations that would be invalid in more normal code
  • You are not even trying to start from the level playing field in terms of garbage in memory, so you may well feel like your first loop doesn't need to do garbage collection, but your last loop does.

You might want to take a look at a microbenchmarking framework designed to prevent this kind of problem, like Caliper or JMH .

+9


source


This is how you explore how adding a final affects performance: http://shipilev.net/blog/2014/all-fields-are-final/



+3


source







All Articles