-1

Using the conditional/ternary operator:

freeCache = (freeCache > (2880 * 3)) ? (2880 * 3) : freeCache;

there is a value assignment in every case.

Using this normal if-Statement:

if(this.freeCache > (2880 * 3)) this.freeCache = (2880 * 3)

there should only be a value assignment if the freeCache value is too high.

So which of those is actually more performant?

Martin Smith
  • 438,706
  • 87
  • 741
  • 845
LostPhysx
  • 3,573
  • 8
  • 43
  • 73
  • 2
    That is not an "inline if". – Oliver Charlesworth Aug 01 '15 at 16:14
  • 2
    I wouldn't bother about "performance" but more about readability first. I doubt this piece of code could be a bottleneck for your application. – Alexis C. Aug 01 '15 at 16:15
  • 2
    Those kinds of micro-optimizations are useless. Use what you find the easier to read, understand and maintain. First thing I would do: declare a constant containing the value of 2880 * 3, with a meaningful name, instead of repeating this magic operation twice. – JB Nizet Aug 01 '15 at 16:16
  • Of course this is an inline if: (statement) ? true: false; see here: "https://en.wikipedia.org/wiki/%3F:" – LostPhysx Aug 01 '15 at 16:16
  • 1
    No, it isn't. It's called the conditional operator. – biziclop Aug 01 '15 at 16:17
  • @biziclop: see the wiki page – LostPhysx Aug 01 '15 at 16:17
  • `if (foo) { callVoidMethod(); } else { return; }` - how do you "inline" that? – Oliver Charlesworth Aug 01 '15 at 16:17
  • 1
    Thank you but I'd rather continue relying on the [Java Language Specification](http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.25) if that's all the same to you. – biziclop Aug 01 '15 at 16:18
  • " It is commonly referred to as the conditional operator, inline if (iif), or ternary if" – LostPhysx Aug 01 '15 at 16:20
  • Stop taking every word too precise just to have something to complain about if you don't know the answer. – LostPhysx Aug 01 '15 at 16:22
  • 1
    @Paedow There is no such thing as "too precise" when it comes to the names of computing terms. But you're right, there are far bigger problems with your question than incorrect wording: firstly that it doesn't matter which one's quicker, both are quick enough, secondly that you should be using meaningful constants instead of magic numbers and thirdly, that the answer will depend on the exact details of the system, you can [measure it yourself](http://stackoverflow.com/questions/504103/how-do-i-write-a-correct-micro-benchmark-in-java) on your own computer if you want to. – biziclop Aug 01 '15 at 16:30
  • 1
    As an aside, you might want to use `this.freeCache = Math.min(this.freeCache, (2880 * 3));` – Alexis C. Aug 01 '15 at 16:33
  • @AlexisC.: is this more or less performant? i have to run this check about 7 billion times every time I run this application – LostPhysx Aug 01 '15 at 16:37
  • 1
    @Paedow Why don't you benchmark both approaches? – Alexis C. Aug 01 '15 at 16:38
  • 1
    @Paedow As I said I don't think this will be a bottleneck in your application. However, if you feel that it could be and you think it's critical, profile your application, benchmark it and see if this is a problematic part. Only proper measurements will tell you the truth about the execution time. – Alexis C. Aug 01 '15 at 16:40
  • i'll try to do this, but i don't really have experience with microtime benchmarking – LostPhysx Aug 01 '15 at 16:44
  • of course I could let the programm completely run with both apporaches but this would take days. And yes, there would be a big difference (since 7 billion times a little is still a lot) – LostPhysx Aug 01 '15 at 16:46
  • Run it for just an hour then and count how many times it executed in that time. – biziclop Aug 01 '15 at 16:49
  • Is there any automation that terminates the program exactly after 1 hour or do I have to do it by myself? – LostPhysx Aug 01 '15 at 17:07
  • You have to do it yourself but it's simple: create a `ScheduledExecutorService` and set it to trigger in an hour's time. You can then collect and print the data you need and call `System.exit()`. (My money is on the JIT compiler optimizing away the single extra `istore` opcode and thus practically identical running times) – biziclop Aug 01 '15 at 17:23
  • @biziclop: some C compilers will optimize a `foo = foo>x ? foo : x` to a conditional-move instruction, but not the `if` version that doesn't have an assignment to `foo` in the other branch. Obviously Java is a completely different beast, and it will depend on the JIT. – Peter Cordes Aug 01 '15 at 19:49

2 Answers2

5

Just for the fun of it, I tried compiling the two suggested implementations (ternary operator vs the explicit if statement). They both use if_icmple instruction so I guess performance will be identical:

The if version:

public static void main(java.lang.String[]);
Code:
   0: iconst_0
   1: istore_1
   2: iload_1
   3: sipush        8640
   6: if_icmple     13
   9: sipush        8640
  12: istore_1
  13: return

Using '?' ternary operator:

public static void main(java.lang.String[]);
  Code:
   0: iconst_0
   1: istore_1
   2: iload_1
   3: sipush        8640
   6: if_icmple     15
   9: sipush        8640
  12: goto          16
  15: iload_1
  16: istore_1
  17: return

There's a slight inefficiency in using the ? operator (At least in this specific case) due to the : clause (the instructions labeled 15 and 16 in the above listing). The JVM will probably optimize away these redundant operations.

nimrodm
  • 23,081
  • 7
  • 58
  • 59
2

They're not even equivalent.

The ternary expression can be expanded to this:

if(freeCache > (2880 * 3)) {
    freeCache = 2880 * 3;
} else {
    freeCache = freeCache;
}

If you're really concerned about performance, there's a[n insignificant] performance hit in that you're performing a redundant assignment again.

In all cases you should strive for readability first. The single if statement is likely a better choice, as that's more readable than the convoluted ternary expression you have.

Makoto
  • 104,088
  • 27
  • 192
  • 230
  • The question itself points out this inequivalency "there is a value assignment in every case." vs "there should only be a value assignment if the freeCache value is too high." – Martin Smith Aug 01 '15 at 16:32
  • At this point, I'm a bit lost. If they know that the ternary will perform an assignment in every case, *and* they have the requirement that it doesn't do that, then there's no reason to debate it - the ternary would not satisfy their needs. – Makoto Aug 01 '15 at 16:35