Any shortcut to 1:length(myVector)?

If your container is a Vector and it is indexed a single time, then there will probably be no difference in performance. Sometimes even if it is indexed multiple times (if the compiler can prove it does not change).

I would go with pairs in this case anyway because I believe the code may change and eachindex may become a worse decision, but I do not see the opposite happening (i.e., I do not see eachindex becoming a better decision than pairs in a code that use both index and value). But if the code is guaranteed to not change too much (i.e., will keep using Vectors and not making so many accesses) then the decision ends up being mostly stylistic. I would only recommend eachindex if you really is semantically iterating positions and may change the value in the indexed position inside the loop before accessing it again and therefore using pairs can lead to a subtle bug of using the old value stored in value instead of the new in the indexed position.

EDIT: updated my first answer to address some cases I thought only after after this question.

1 Like