-3

This is something I was thinking, when I searched at Google I couldn't find the answer (maybe I don't know the keywords). Just at a curiosity level, no implementation in any project.

Which is faster:

if (bool)
   return true;
else
   return false;

or

bool ? true : false;
  • Are they equal?

  • Why?

  • One is faster than another in every language, in every system?

(If someone knows the answer for microcontrollers, Obj-C for iOS or Java, I would really appreciate your answer to my question)

EDIT: I didn't know that bool ? true : false is called ternary, as I said "I don't know the keywords".

GBF_Gabriel
  • 2,636
  • 2
  • 17
  • 15

8 Answers8

4

This Question has a number of problems.

  1. First, languages don't have performance characteristics. You cannot measure the performance of a language. You can only measure an implementation of a language. And any given language can have lots of different implementations.

  2. The idea that all languages will support constructs equivalent to those two is naive ... and incorrect.

  3. The idea that something might perform the same way across all languages and all implementations and all systems, is fanciful.

  4. The idea that anyone would know ... for all languages, platforms, hardware, etcetera is fanciful.

  5. It is unclear what those two utterances mean. In Java (for example) they don't mean the same thing ... the second one is not a valid statement ... even if you add a ; ... and even if it was, it doesn't return anything.


Having said that ... in Java (assuming you add a return in the 2nd case):

  • they mean the same thing, and

  • a modern Hotspot JIT compiler is likely to compile those to equivalent native code; i.e. there is likely to be no performance difference.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • +1 When comparing the performance of code which hasn't been optimised, the most important thing is whether the compiler can detect the redundant code and remove it. – Peter Lawrey Jun 30 '13 at 16:12
1

In Java, at least, there's no difference between them other than the ability to debug line-by-line in the first case. The compiled bytecode is most likely exactly the same for both cases. I suspect the same is true in every environment you are asking about.

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
1

What is usually faster is

return bool;

This avoids any possible branching produced by the conditional, being faster on a CPU with expensive branch misses. An optimizing compiler might remove the branch anyway, but since this is language agnostic it can't be guaranteed.

Joni
  • 108,737
  • 14
  • 143
  • 193
  • You're assuming that `yes` and `no` are boolean values. – Ted Hopp Jun 30 '13 at 15:21
  • ...and that is pretty unlikely ;) – snøreven Jun 30 '13 at 15:22
  • 1
    @TedHopp, @snoreven, the OP explicitly said that `yes` and `no` are `true` and `false` in his comments to the question. – Bruno Reis Jun 30 '13 at 15:23
  • YES and NO are the boolean values in Objective C, one of the languages mentioned by the asker. – Joni Jun 30 '13 at 15:25
  • Maybe in ONE of the languages.. normally I don't assume "yes" is a boolean value... – snøreven Jun 30 '13 at 15:28
  • @snøreven, did you read the OP's comment on the question I just mentioned? He says: *@JonSkeet just the return value, same as true and false.*. The OP assumes `yes` is indeed the boolean value `true`, that's the hypothesis for this question. Whether you or I would assume that or not, whether it is a good or bad assumption, is a different problem. – Bruno Reis Jun 30 '13 at 15:29
  • @bruno-reis: I did, I was just responding to the obj C comment because of the strange argument that "YES" is a bool in one language ;) – snøreven Jun 30 '13 at 15:31
  • Objective C is not so well known that it would be used as a "random" example, so there's your first hint. Second, reading the title of the question also gives a hint that a boolean should be returned. – Joni Jun 30 '13 at 15:44
1

The speed difference occurs at the machine code level, and at that level it can't even be guessed which of your idioms compiled into it. Therefore your answer is: there is no correlation between the choice of your idioms and performance. Mostly it should be exactly the same, but if it isn't, there will be no generally applicable explanation.

Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
1

The conditional expression versus the ternary is debatable. This is one of those ones where you shouldn't be able to notice a difference in either case.

If you have a single if/else (which that should be, there is no need for an else if in there), the Ternary will be faster.

However, it is more difficult to add additional checks, since you need to chain the ternary: (conditional) ? ((conditional) ? true : false) : ((conditional) ? true : false) so it becomes difficult to read.

The best choice if expansion is possible is to use a switch which is faster than an if/else check. This is especially true if you want to allow multiple if's to result in the same result, but is not useful if you're using datatype checking since it uses loose comparisons (== and never === checks). Unlike an if/elseif/else pair, the switch can potentially use more memory and time if you never break.

Nidhish Krishnan
  • 20,593
  • 6
  • 63
  • 76
0

Performance wise both Ternary and if-else are same.

Ternary operators are just shorthand. They compile into the equivalent if-else statement.

Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115
0

Are they equal?

Logically yes. The do the same thing here. The second one is called ternary operator.

One is faster than another in every language, in every system?

In java there is no difference. I doubt if there is any performance in other language also. Even if so, then I guess the difference will be very negligible. You better concentrate on more readable codes. The first one is definitely more readable.

stinepike
  • 54,068
  • 14
  • 92
  • 112
0

They are pretty much equal in performance, or if not there is a very, VERY trivial difference.

For something simple like what you asked for I would use the ternary operator ?: because it is much shorter and still very readable.

When you need to do multiple statements, use if-else.

Remember that you can also use ?: in return statements:

return bool ? yes : no;

EDIT: It looks like you just want to return the value of the boolean. If you do then all you have to type is: return bool;

Dynomyte
  • 181
  • 4