lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


2012/12/20 Vadim Peretokin <vperetokin@gmail.com>:
> Don't measure them, then. But if you're doing a lot of loops that do little
> work, you'd want to know which is the best to use.

The point of my post is that overhead cost is not the reason why
code is slow. Therefore choosing between `while`, `ipairs` and `for`
on the basis of performance is, to quote Andres, "grade-A ricer"
reasoning.

Instead, one should use the freedom of choice to promote
readability and maintainability.

For what it's worth, I use the following conventions:

1. `while` when the loop test is not simply whether
    a variable constantly incremented by has passed
    a cutoff value.
2. `repeat` like `while`, for cases when a test at the end
    fits more logically into the structure.
3. `ipairs` when the iteration runs over exactly one
    table.
4. Numeric `for` when there is no table involved, or
    when more than one table is indexed by a key
    dependent on the loop variable.
5. Generic `for` when I have PiL to hand and a relaxed
    hour or two available.
6. Coroutines are on my future to-be-mastered list.