-6

Which way is quicker and less resource intensive:

boolean = !boolean
boolean ^= boolean

Edit: This is not an opinion based question. There IS a correct answer.

  • 8
    This is a meaningless nano-optimization. Write the one that's most readable and easily understood by normal human beings. – duffymo Jan 18 '16 at 17:57
  • 1
    This is NOT a legitimate question. And it's even a duplicate http://stackoverflow.com/questions/224311/cleanest-way-to-toggle-a-boolean-variable-in-java?rq=1 – Idos Jan 18 '16 at 17:59
  • Hardly matters in any situation. – kbz Jan 18 '16 at 18:00
  • 4
    I don't see a practical use for this knowledge (if you have one please post), but as a general curiosity it seems an _ok_ question. It's clear, has a code example and the answer is not a matter of opinion. I also don't see why it has so many downvotes – jonk Jan 18 '16 at 18:04
  • 2
    @Idos it is not a duplicate. Your link does not give the answer to which of the two operators in this question requires less performance. – RaminS Jan 18 '16 at 18:05

2 Answers2

6

I measured with the following code.

public static void main(String[] args)
{

    boolean myVariable = true;
    long startTime = 0;
    long endTime = 0;
    long duration1 = 0;
    long duration2 = 0;


    for(int i=0; i<1000; i++) {
        startTime = System.nanoTime();
        myVariable = !myVariable;
        endTime = System.nanoTime();

        duration1 += (endTime - startTime);


        startTime = System.nanoTime();
        myVariable ^= myVariable;
        endTime = System.nanoTime();

        duration2 += (endTime - startTime);

    }

        System.out.println("The duration for the first operation is :" + (duration1/1000));
        System.out.println("The duration for second operation is :" + (duration2/1000));
}

and the results are (in nanoseconds)

The duration for the first operation is :140

The duration for the second operation is :123

Based on this analysis, the boolean ^= boolean is quicker than the boolean = !boolean.

Bernhard Colby
  • 311
  • 5
  • 17
  • 1
    I guess if you do `boolean ^= boolean` first, then the `boolean = !boolean` would be quicker. – Jakuje Jan 18 '16 at 18:49
  • @Jakuje boolean ^= boolean is already quicker. I changed their places and boolean ^= boolean is still quicker. – Bernhard Colby Jan 18 '16 at 18:53
  • 1
    ok. It was attempt to joke against java. But yes, it makes sense. – Jakuje Jan 18 '16 at 18:55
  • 1
    @Jakuje :) When I changed their places, boolean ^= boolean completed in 78 nanoseconds and the other 115 nanoseconds. Of course, that depends on the power of the local machine but somehow, we should trust on the statistics :) – Bernhard Colby Jan 18 '16 at 18:57
  • 1
    Excellent test! Thank you for the code. I just recently found out about Xor boolean swap and was curious which would be better..even if it is in nanoseconds. – by this guy. Jan 19 '16 at 02:02
1

boolean = !boolean is probably better since it's easier to read. Both are not exactly resource intensive so using either one wouldn't really make a real difference in the performance.

J J
  • 93
  • 6