javajvmmicro-optimizationpremature-optimizationmicrobenchmark

Additional 'if checks' if the value is already set up - what is faster, what uses more resources?


Assume that we have a given interface:

public interface StateKeeper {

    public abstract void negateWithoutCheck();

    public abstract void negateWithCheck();

}

and following implementations:

class StateKeeperForPrimitives implements StateKeeper {
    private boolean b = true;

    public void negateWithCheck() {
        if (b == true) {
            this.b = false;
        }
    }

    public void negateWithoutCheck() {
        this.b = false;
    }
}

class StateKeeperForObjects implements StateKeeper {
    private Boolean b = true;

    @Override
    public void negateWithCheck() {
        if (b == true) {
            this.b = false;
        }
    }

    @Override
    public void negateWithoutCheck() {
        this.b = false;
    }
}

Moreover assume that methods negate*Check() can be called 1+ many times and it is hard to say what is the upper bound of the number of calls.


Solution

  • There might be a slight performance benefit in using the one with the check. I highly doubt that it matters in any real life application.

    premature optimization is the root of all evil (Donald Knuth)


    You could measure the difference between the two. Let me emphasize that these kind of things are notoriously difficult to measure reliably.

    Here is a simple-minded way to do this. You can hope for performance benefits if the check recognizes that the value doesn't have to be changed, saving you an expensive write into the memory. So I have changed your code accordingly.

    interface StateKeeper {
    
        public abstract void negateWithoutCheck();
    
        public abstract void negateWithCheck();
    
    }
    
    class StateKeeperForPrimitives implements StateKeeper {
    
        private boolean b = true;
    
        public void negateWithCheck() {
            if (b == false) {
                this.b = true;
            }
        }
    
        public void negateWithoutCheck() {
            this.b = true;
        }
    }
    
    class StateKeeperForObjects implements StateKeeper {
    
        private Boolean b = true;
    
        public void negateWithCheck() {
            if (b == false) {
                this.b = true;
            }
        }
    
        public void negateWithoutCheck() {
            this.b = true;
        }
    }
    
    public class Main {
    
        public static void main(String args[]) {
    
            StateKeeper[] array = new StateKeeper[10_000_000];
    
            for (int i=0; i<array.length; ++i)
                //array[i] = new StateKeeperForObjects();
                array[i] = new StateKeeperForPrimitives(); 
    
            long start = System.nanoTime();
    
            for (StateKeeper e : array)
                e.negateWithCheck();
                //e.negateWithoutCheck();
    
            long end = System.nanoTime();
    
            System.err.println("Time in milliseconds: "+((end-start)/1000000));
        }
    }
    

    I get the followings:

               check  no check
    primitive   17ms    24ms
    Object      21ms    24ms
    

    I didn't find any performance penalty of the check the other way around when the check is always superfluous because the value always has to be changed.

    Two things: (1) These timings are unreliable. (2) This benchmark is far from any real life application; I had to make an array of 10 million elements to actually see something.

    I would simply pick the function with no check. I highly doubt that in any real application you would get any measurable performance benefit from the function that has the check but that check is error prone and is harder to read.