1

So many times I have come across the question of How do I implement this conditional setting?. I know this might sound really confusing, so let me explain what I mean using code.

Essentially, I want to know which of the following would allow for the fastest execution time and least memory consuming, and why this is the case (hopefully with using references to the stack and/or registers - and possibly the components of the computer such as the time it takes for the ALU to process the comparison every time, verses the time it takes for the CU to store the variable every time).

// Variables won't be like this all the time...
boolean toggles = false;
boolean pressed = true;

void isPressed() {
    boolean ret = pressed;

    // Case one - test for toggling
    if(!toggles) pressed = false;

    // Case two - test for toggling and if it's true
    if(!toggles && pressed) pressed = false;

    return ret;
}

Some background on the situation: I'm designing how key combinations could potentially be implemented into a game but I want to make the Combination class as generic as I can rather than having two separate classes such as CombinationToggle and CombinationAlwaysOff, however, I - and probably many others - have come across this sort of situation.

I'm trying to determine that if this specific combination is toggled, and if it's not, turn the pressed boolean off. But I'm unsure as to whether I should include a test to determine if it needs to be turned off.

Thilina Sampath
  • 3,615
  • 6
  • 39
  • 65
TheBrenny
  • 528
  • 3
  • 19
  • I think case one is faster because of Branch Prediction [https://en.wikipedia.org/wiki/Branch_predictor](https://en.wikipedia.org/wiki/Branch_predictor) – kyriosli Oct 30 '15 at 03:52
  • Why not `pressed = toggles;`? The speed of assignment to a `boolean` is negligible. Also, setting is setting. Comparing is comparing. You seem to really be asking if it's better to set or test that it isn't already set. This smells of premature optimization. But adding a short-cirtcuit return is one way to do that. – Elliott Frisch Oct 30 '15 at 03:56
  • Maybe, this will help you: http://stackoverflow.com/questions/910423/which-is-faster-comparison-or-assignment or http://stackoverflow.com/questions/4043455/which-costs-more-while-looping-assignment-or-an-if-statement – Yeldar Kurmangaliyev Oct 30 '15 at 04:02
  • 7
    The differences that you are talking about are so minute that it is unlikely that there would be a perceivable performance difference in a real application. Java is a high level language ... you really want to be talking about "profiling and optimization when needed" rather than "how many ALU instructions are needed." See also: short circuit evaluation. – scottb Oct 30 '15 at 04:09
  • 3
    Couldn't agree more with @scottb. The answer would depend on the platform being used. In-fact the JVM (which is of-course platform dependant) already does optimizations under the hood to make your code run faster. It changes instructions appropriately to make the best use of your system's architecture. – TheLostMind Oct 30 '15 at 04:18

0 Answers0