2

This doesn't look like a duplicate, as only one my solutions involves a branch.

Essentially, which of these two lines is more efficient? will be a java app, but it'd be nice to know a general answer well.

shouldRefresh = useCache ? refetchIfExpired : true;

shouldRefresh = !useCache || refetchIfExpired;
Ky -
  • 30,724
  • 51
  • 192
  • 308
  • 1
    They should be identical. – Mike Dunlavey Jan 03 '14 at 22:22
  • 3
    Have you tried javap to disassemble the the resulting code? And then you could look at what the HotSpot compiler produces on several platforms. I would not believe any statements here that I had not made up myself :-) – Harald Jan 03 '14 at 22:23
  • 6
    Why does it matter? The question "Is this code easy to understand?" is so much more important than the particular efficiency of a single line that it's impossible to overstate. – Chris M. Jan 03 '14 at 22:23
  • **Please don't worry about "fastest" without having first done some sort of measurement that it matters.** Rather than worrying about fastest, think about which way is clearest. – Andy Lester Jan 03 '14 at 23:06
  • That doesn't look like a duplicate, as both solutions involve a branch – Ky - Jan 04 '14 at 15:20

2 Answers2

5

The JIT compiler will figure out the fastest operation and use that. Use whatever makes the most sense to read. Don't optimize prematurely.

For interest's sake: If this were being compiled without optimizations, then the boolean operator would be faster. It's a simple mathematical operation, which takes just one CPU cycle (plus another for the ! operator), whereas the ternary expression would require a branch, which interrupts the pipeline if branch prediction guesses wrong.

StriplingWarrior
  • 151,543
  • 27
  • 246
  • 315
1

I would not care about performance here, but about readability. With this aspect, the ternary operator wins in your example. By the way, I expect roughly the same performance.

You can also look how readability helps to save time in maintenance of code. So what is more important? Almost unmeasurable micro-optimization or easier understanding? And when you think a comment shall fix this, so I consider this as an unnecessary writing effort which also costs time.

Meno Hochschild
  • 42,708
  • 7
  • 104
  • 126
  • Readability is easily fixed by a comment, which doesn't affect performance – Ky - Jan 03 '14 at 22:39
  • 1
    Why would you say that ternary wins at readability? – Louis Wasserman Jan 03 '14 at 23:03
  • @LouisWasserman At least speaking for me, the evaluation of logical and boolean operators put slightly more mental stress upon me. If you are lucky with this style then fine for you ;-) – Meno Hochschild Jan 03 '14 at 23:17
  • @Supuhstar: Then you're forcing someone to read and maintain both the code and the comments, and sometimes the comments don't match what the code does. Far better to write code that is easy to understand in the first place. In most cases, that will not have a noticeable detrimental effect on performance. – StriplingWarrior Jan 03 '14 at 23:26