Skip to content

Comment on Which is faster? while(1) {} or while(2) {}

Comments

Is it just me, or is most everyone missing the point that neither of these terminate, and therefore both take "infinite time."

It seems we're all getting hung up on whether evaluating the truthiness of 1 or 2 is faster, but that's really immaterial.

I think it's supposed to be implied that stuff goes inside the loop:

while (1) { // code goes here }

Devil's advocate - if for some reason you're using "while(<constant evaluating to true>)" to do something other than wait for some kind of event, and if whatever you're doing is fast but repeated a huge number of times, it's possible that you'd see some speedup in the overall program by using a "faster" conditional evaluation (assuming that it's actually evaluated at runtime and not compiled away because it's a constant value).

I am having a really, really hard time contriving a reason for this, though. It may be my lack of imagination, but I feel like the only reason this would come up is if you deliberately designed a system specifically to make this an issue.

Honestly, you could take this question a bunch of ways. Maybe he's just trying to ask about whether the speed of a cast-to-bool depends on the value of the thing you're casting, maybe he's trying to get you to see that the compiled code won't even evaluate the conditional at runtime anyway or maybe he's trying to get you to realize that you'd almost certainly not care because even if it were evaluating the conditional, if you're using while(<true>), you're almost certainly waiting for some event, so the only thing it could possibly affect would be polling speed, negligibly.

while() doesn't cast to bool in C. It compares to integer 0 which will become an unconditional jump after optimization.

The question can be interpreted in different ways. For example: (1) "Which of these infinite takes the longest to run to completion?". Another interpretation could be: (2) "We want to write an infinite loop, which of these two ends up with the most efficient code?".

I don't think anyone is missing the point that an infinite loop takes infinite time to terminate, it's just not a very interesting point to make. Getting "hung up" on (1) means missing out on potentially interesting insights found when answering (2).

The author of the question stated that the interviewer said that while(1) was faster. Probably the interviewer really wanted to get into the evaluation matters. Questions like this, especially during work interviews, make me think that interviewers read some "fun facts" somewhere and decided to ask them during the interview instead of actually evaluating the candidate's skill. Sorry for the rant.

the moon orbits around the earth for an (effectively) infinite amount of time - that doesn't mean that how fast it goes isn't interesting.

AboutSource Built by g1lg1l

Hackerly is an independent reader for Hacker News, built on the public HN API. Not affiliated with Y Combinator.