writing infinite loop
This was asked from my friend in an interview.
Which is a efficient way of writing an infinite loop??
I guess all are same, what do you think guys?
I'm not sure what an "efficient" way would be to writing an infinite loop, since it is an infinite loop, but I'd guess: while(1){ } or for(;;){ } So yea, the question needs more context to make sense. (The question they asked, not you)
Is while(true); more efficient than while(1); ?
I would guess so but im not sure, my theory is that 1 would have to evalute to true first, not sure about for(;;);
I would guess so but im not sure, my theory is that 1 would have to evalute to true first, not sure about for(;;);
Is while(true); more efficient than while(1); ?
I would guess so but im not sure, my theory is that 1 would have to evalute to true first, not sure about for(;;); or maybe the bool would have to change to an int type.
I would guess so but im not sure, my theory is that 1 would have to evalute to true first, not sure about for(;;); or maybe the bool would have to change to an int type.
Quote:Original post by simon10k
Is while(true); more efficient than while(1); ?
I would guess so but im not sure, my theory is that 1 would have to evalute to true first, not sure about for(;;);
No. Even if true was a macro (or even a constant), it would be replaced at compile-time.
void slow_infinite_loop(){ double f = 0; while(f < 1) { f = rand_float(); f = secant(f) * cosine(f); f /= 2.0; //do stuff here }}
If the infinite loop is intentional, then chances are it won't get much faster than it already is.
CM
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement