4 ms·
Wouldn't it be better to write for (size_t i = size; i > 0; i--) { // ... } and use i-1 inside the loop instead of using underflow?
by croes 5y ago
Wouldn't it be better to write
for (size_t i = size; i > 0; i--) {
// ...
}
and use i-1 inside the loop instead of using underflow?
- xxs 5y agoor: "for (size_t i = size; i-- > 0;){...}"
- desdiv 5y agoI foresee two problems with that: Problem 1: If i is used multiple times inside the loop, then you might need to make a temporary variable for it, making it confusing: for (size_t i_plus_one = size; i_plus_one > 0; i_plus_one--) { size_t i = i_plus_one - 1; f(i); g(i); h(i); ... (The alternative is a bunch of i-1 all over the place, which is also confusing.) Problem 2: If operations inside the loop is short, then the extra "i-1" arithmetic could result in unacceptable performance penalties.