- 2
Which one is faster ?
while (1) Or while (true) ? (C++)
14 ответов
- 11
Arsenic If you make a game then there is 3 steps
First is skeleton means backbone of game
Second is looping
Third is events
The second step is done by loops and main loop is "while True :" loop
If you don't know just check on the google......
+ 9
Mani_K_A
Please mention the language because in some languages while(1) will give error.
+ 8
There is no difference in while (1) and while (true) . In some languages 1 and 0 are also consider as Boolean value.
Performance depends on many factors like too many if else, unnecessary condition, unnecessary loops, unused variable, unnecessary objects, database connection is open but didn't close and many more.
+ 7
Aerith I didn't got your point.
How does one can't use multiple loops and/or function calls inside the other loop ?
And what is a "make game's loop" ? Never heard that term before.
+ 5
Loop performance more likely depend on how "heavy" the workload put inside the loop body rather than the loop condition evaluation. For those two examples, there is no complexity in condition evaluation, I suppose.
+ 3
Continue......
Real time example:
One of my colleagues used two loop of same collection for different purpose so if there thousands records are coming from database then think where should take 10 minutes for a single operation will take 10 minutes more.
+ 2
Aerith
Arsenic certainly knows and doesn't need to check on Google. You on the other hand, need to be less 'confidently incorrect'.
Firstly, the OP never mentioned games. Why are talking about game loops here? Game loops are technically the same as any other loops, and there are zillions of scenarios where seemingly infinte while loops are used, not just games.
Secondly, 1 is the same as true in some languages and a truthy value in others. So while(1) in place of while(true) is totally valid in most languages, and while( (bool) 1 ) will work fine in most others.
Mani_K_A
I can't sat for sure that while(1) and while(true) aren't different of performance, but I can say that in most cases, the performance difference won't really matter much. I still suggest you use while(true) as personally I feel that it is more self-explanatory, but you can decide yourself. As Ipang said, loop performance depends more on what's inside the loop.
+ 2
some languages while (1) will be give error
+ 1
Both are same
- 1
The best way to solve this would be to create a variable such as boolean isRunning = true;
Then when you make your loop while(isRunning) will always evaluate to true, and this with inside the loop if you ever want to exit you can evaluate isRunning = false; then you will leave the loop.
To answer your question, they will all be the same speed unless you use a different type of format or loop.
- 1
The two loops are exactly the samy, because your (good) compailer will replace true into 1 (like constexpr)
- 3
While(True) loop is faster because if you want to take all the things one by one and you can also put more loops and function in it........
Also while True loop is used in make game's loop....
- 3
And you can stop the while True loop whereas conditional while loop stops when the condition gets false....
- 3
Thanks for giving my answer best