2

So what is the logic to say the the default equals(Object obj)

public boolean equals(Object obj){
  this == obj; 
}

or:

int a = 1;
int b = 2;
a == b;

Could be another type with another value, I just don't find any documentation in what the cost of each one is.

Adding a reason for this:

I was reading this question and I was thinking, if there is an optimization for comparing for example a value inside the object (like an int) and compare them instead of the reference

Debosmit Ray
  • 5,228
  • 2
  • 27
  • 43
developer_hatch
  • 15,898
  • 3
  • 42
  • 75
  • Guessing... checking referential equality is less overhead than value equality – OneCricketeer Apr 12 '18 at 03:16
  • Yeah... I had the same thought but... I really can't be able to have some facts on it, just frustrating – developer_hatch Apr 12 '18 at 03:17
  • Try inspecting the byte-code? – OneCricketeer Apr 12 '18 at 03:19
  • Who said there's a difference? And why would it matter? – shmosel Apr 12 '18 at 03:21
  • 1
    I would imagine integer would be faster as you are comparing a 32bit integer vs a 64 bit address – Mitch Apr 12 '18 at 03:21
  • 1
    @Mitchel0022 Object references aren't necessarily 64-bit. – shmosel Apr 12 '18 at 03:22
  • 3
    I'm wondering - what difference answer will make? if you have to compare integers - compare integers, if you have references - compare references, you cannot replace one comparison with another, so it does not matter what is faster, you have to use what you have – Iłya Bursov Apr 12 '18 at 03:25
  • @IlyaBursov because of this question I read [https://stackoverflow.com/questions/49786873/comparator-interfaces-equals-method-why-it-is-always-safe-not-to-override-obje#comment86589421_49786873] – developer_hatch Apr 12 '18 at 03:27
  • 4
    Though the answer is totally JVM-dependent, it's likely the JIT-compiled code for both will be similar: one instruction to compare and another to branch. It will be an extremely rare use case where any difference that does occur is important. – Gene Apr 12 '18 at 03:27
  • @shmosel It is relevant in term of possible efficiency (at least that's the whole point of my research) – developer_hatch Apr 12 '18 at 03:31
  • Why not just measure it, instead of asking? – Mark Bolusmjak Apr 12 '18 at 03:33
  • @z5h that's a good point, I could, but I'm looking an explaination of why, and not just the fact. – developer_hatch Apr 12 '18 at 03:35

1 Answers1

1

Wrote a little class (feel free to run in your editor of choice)..

public class HelloWorld {

    public static void main(String []args) {
        HelloWorld app = new HelloWorld();

        int n = 100000;

        long startTime = System.nanoTime();
        app.doEqualsNull(n);
        long endTime = System.nanoTime();
        System.out.println("doEqualsNull (ns): " + (endTime - startTime)) ; 

        startTime = System.nanoTime();
        app.doEqualsThis(n);
        endTime = System.nanoTime();
        System.out.println("doEqualsThis (ns): " + (endTime - startTime)) ;

        startTime = System.nanoTime();
        app.doEqualsSameInt(n);
        endTime = System.nanoTime();
        System.out.println("doEqualsSameInt (ns): " + (endTime - startTime)) ;

        startTime = System.nanoTime();
        app.doEqualsDifferentInt(n);
        endTime = System.nanoTime();
        System.out.println("doEqualsDifferentInt (ns): " + (endTime - startTime)) ;
    }

    public void doEqualsNull(int n) {
        for (int i = 0 ; i < n ; i++) {
            this.equals(null);
        }
    }

    public void doEqualsThis(int n) {
        for (int i = 0 ; i < n ; i++) {
            this.equals(this);
        }
    }

    public boolean equals(Object obj){
        return this == obj; 
    }

    public void doEqualsSameInt(int n) {
        for (int i = 0 ; i < n ; i++) {
            this.equalsInt(5);
        }
    }

    public void doEqualsDifferentInt(int n) {
        for (int i = 0 ; i < n ; i++) {
            this.equalsInt(50);
        }
    }

    public boolean equalsInt(int other){
        return 5 == other; 
    }
}

Output:

doEqualsNull (ns): 3660106
doEqualsThis (ns): 3237291
doEqualsSameInt (ns): 2887368
doEqualsDifferentInt (ns): 2992786

Integer equality seems to take slightly shorter time (it's really really small difference) -- but again, I am guessing some branch prediction did kick into my super scientific calculations. :)

Debosmit Ray
  • 5,228
  • 2
  • 27
  • 43