3 ms·
Using % vs comparison and reset was probably more efficient with the compilers of the time; might still be. Also I doubt everyone would agree that the one exit
by _flux 3y ago
Using % vs comparison and reset was probably more efficient with the compilers of the time; might still be.
Also I doubt everyone would agree that the one exit condition is more clear.
For example, if I know that if e->inuse is 0 it will continue with the next round. Arguably it's more difficult to understand that from the single combined expression—because it isn't so: if !e->inuse but e == ent->client->chase_target, the original will loop but yours will exit.
Though I'm guessing ent->client->chase_target->inuse is probably never 0.
- augustk 3y ago> if !e->inuse but e == ent->client->chase_target, the original will loop but yours will exit. No, the continue statement will jump to the test part also for `do' loops: "The continue statement is related to break, but less often used; it causes the next iteration of the enclosing for, while, or do loop to begin. In the while and do, this means that the test part is executed immediately" -- "The C programming language (Second Edition)"
- _flux 3y agoWell today I have learned something I may have forgotten, thank you :). It seems natural in for and while so it does make sense also the do loops use it.
- scott_s 3y agoSeconded that the main reason was probably avoiding a mod. But it's not because of the compiler, but the processor. Mod and division instructions are always more expensive than additions and multiplications. If this function was on the hot path (and looking at where it's called, it's at least called once per frame), that may have mattered a lot.
- Sharlin 3y agoA modern optimizing compiler would quite likely do induction variable elimination/strength reduction and replace the % with increment-and-reset if it deemed it more efficient, so it’s also about the compiler.