5w3rv0
5w3rv0

Reputation: 67

Performance difference between assignment and conditional test

This question is specifically geared towards the Java language, but I would not mind feedback about this being a general concept if so. I would like to know which operation might be faster, or if there is no difference between assigning a variable a value and performing tests for values. For this issue we could have a large series of Boolean values that will have many requests for changes. I would like to know if testing for the need to change a value would be considered a waste when weighed against the speed of simply changing the value during every request.

public static void main(String[] args){
    Boolean array[] = new Boolean[veryLargeValue];
    for(int i = 0; i < array.length; i++) { 
        array[i] = randomTrueFalseAssignment;
    }
    for(int i = 400; i < array.length - 400; i++) { 
        testAndChange(array, i);
    }
    for(int i = 400; i < array.length - 400; i++) {
         justChange(array, i);
    }
}

This could be the testAndChange method

public static void testAndChange(Boolean[] pArray, int ind) {
    if(pArray) 
        pArray[ind] = false;
}

This could be the justChange method

public static void justChange(Boolean[] pArray, int ind) {
    pArray[ind] = false;
}

If we were to end up with the very rare case that every value within the range supplied to the methods were false, would there be a point where one method would eventually become slower than the other? Is there a best practice for issues similar to this?

Edit: I wanted to add this to help clarify this question a bit more. I realize that the data type can be factored into the answer as larger or more efficient datatypes can be utilized. I am more focused on the task itself. Is the task of a test "if(aConditionalTest)" is slower, faster, or indeterminable without additional informaiton (such as data type) than the task of an assignment "x=avalue".

Upvotes: 0

Views: 720

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 477210

As @TrippKinetics points out, there is a semantical difference between the two methods. Because you use Boolean instead of boolean, it is possible that one of the values is a null reference. In that case the first method (with the if-statement) will throw an exception while the second, simply assigns values to all the elements in the array.

Assuming you use boolean[] instead of Boolean[]. Optimization is an undecidable problem. There are very rare cases where adding an if-statement could result in better performance. For instance most processors use cache and the if-statement can result in the fact that the executed code is stored exactly on two cache-pages where without an if on more resulting in cache faults. Perhaps you think you will save an assignment instruction but at the cost of a fetch instruction and a conditional instruction (which breaks the CPU pipeline). Assigning has more or less the same cost as fetching a value.

In general however, one can assume that adding an if statement is useless and will nearly always result in slower code. So you can quite safely state that the if statement will slow down your code always.

More specifically on your question, there are faster ways to set a range to false. For instance using bitvectors like:

long[] data = new long[(veryLargeValue+0x3f)>>0x06];//a long has 64 bits

//assign random values

int low = 400>>0x06;
int high = (veryLargeValue-400)>>0x06;
data[low] &= 0xffffffffffffffff<<(0x3f-(400&0x3f));
for(int i = low+0x01; i < high; i++) {
    data[i] = 0x00;
}
data[high] &= 0xffffffffffffffff>>(veryLargeValue-400)&0x3f));

The advantage is that a processor can perform operations on 32- or 64-bits at once. Since a boolean is one bit, by storing bits into a long or int, operations are done in parallel.

Upvotes: 1

Related Questions