Thursday, 4 May 2017

c - Which is faster: while(1) or while(2)?

This was an interview question asked by a senior manager.



Which is faster?



while(1) {
// Some code

}


or



while(2) {
//Some code
}



I said that both have the same execution speed, as the expression inside while should finally evaluate to true or false. In this case, both evaluate to true and there are no extra conditional instructions inside the while condition. So, both will have the same speed of execution and I prefer while (1).



But the interviewer said confidently:
"Check your basics. while(1) is faster than while(2)."
(He was not testing my confidence)



Is this true?



See also: Is "for(;;)" faster than "while (TRUE)"? If not, why do people use it?

No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...