Complexity: Conditional operator vs if-else

Journey Woo :

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 :-)

Zabuza :

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.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=135529&siteId=1