2

I am working on a simple code algorithm with Java, and I am wondering there may be a difference between ? : and if-else when calculating the time complexity. I think they both have the same time complexity, but I am not sure...

For example:

if (number == num) {
    count += 1;
}
else {
    count -= 1;
}

and

count += (number == num) ? 1 : -1;

Really thank you if you can tell me the difference between them :-)

Tozz
  • 256
  • 1
  • 5
  • 19
  • 1
    What makes you think that there would be any difference regarding time complexity? Maybe you have understood something wrong and we can clear the confusion then. – Zabuzard Jun 05 '19 at 14:12
  • 1
    The proposed duplicate is very related, but talks about actual time, not complexity. – Zabuzard Jun 05 '19 at 14:17
  • 2
    Note that "time complexity" is something very different than "time it takes". If you create a new operator that first sleeps for one hour and then runs the same if check the "time complexity" will still be constant / O(1). – luk2302 Jun 05 '19 at 14:18
  • @Zabuza I ***highly*** doubt OP actually wanted to know about complexity theory. – luk2302 Jun 05 '19 at 14:19

1 Answers1

2

There is no difference, both constructs are in Theta(1), so constant time.

Not talking about their content, obviously. But in your case even the content is in constant time. So both of your snippets run in Theta(1) time.

Zabuzard
  • 25,064
  • 8
  • 58
  • 82