# Best practices

**URL:** <https://discourse.julialang.org/t/best-practices/85138>\
**Category:** Numerics\
**Tags:** performance\
**Created:** [August 2, 2022, 3:24am UTC](https://discourse.julialang.org/t/best-practices/85138 "2022-08-02T03:24:08Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dario-Rosa85](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dario-rosa85/32/32358_2.png) [@Dario-Rosa85](https://discourse.julialang.org/u/Dario-Rosa85)\
**Post date:** [August 2, 2022, 3:24am UTC](https://discourse.julialang.org/t/best-practices/85138/1 "2022-08-02T03:24:08Z")

</div>

I am running a code which is diagonalizing very large matrices, hence I need to reach the best possible performance. when scanning a vector I was using the following syntax

```julia
for i in 1:length(vec)

```

which works fine. However, I am recently receiving the following warning

> Indexing with indices obtained from `length`, `size` etc is discouraged. Use `eachindex` or `axes` instead.

Is it just a good practice? or the performance is also affected?

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [August 2, 2022, 5:32am UTC](https://discourse.julialang.org/t/best-practices/85138/2 "2022-08-02T05:32:33Z")

</div>

It is best practice. I am not sure if it improves performance, but it is conceivable. It will certainly not harm it.

At any rate, it is strongly recommended, and will improve the safety and generality of your code.

---

<div class="post-metadata">

**Author:** ![Dario-Rosa85](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dario-rosa85/32/32358_2.png) [@Dario-Rosa85](https://discourse.julialang.org/u/Dario-Rosa85)\
**Post date:** [August 2, 2022, 5:40am UTC](https://discourse.julialang.org/t/best-practices/85138/3 "2022-08-02T05:40:22Z")

</div>

thank you

---

<div class="post-metadata">

**Author:** ![RayleighLord](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rayleighlord/32/33592_2.png) [@RayleighLord](https://discourse.julialang.org/u/RayleighLord)\
**Post date:** [August 2, 2022, 7:06am UTC](https://discourse.julialang.org/t/best-practices/85138/4 "2022-08-02T07:06:57Z")

</div>

Using `eachindex` ensures that your code can work with arrays that do not necessarily start with index 1. These types of arrays can be created using OffsetArrays.jl.

---

<div class="post-metadata">

**Author:** ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)\
**Post date:** [August 2, 2022, 8:13am UTC](https://discourse.julialang.org/t/best-practices/85138/5 "2022-08-02T08:13:26Z")

</div>

If you’re diagonalizing large matrices, odds are the diagonalization is the performance critical part, not the way you index into your vectors.

That said, I’ve been taught iteration is better than indexing (they are the same thing for normal arrays, but it’s a good habit):

```julia
for item in vec

```

Or, if you need a counter for some reason

```julia
for (i, item) in enumerate(vec)

```

---

<div class="post-metadata">

**Author:** ![jdad](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jdad/32/4739_2.png) [@jdad](https://discourse.julialang.org/u/jdad)\
**Post date:** [August 2, 2022, 9:28am UTC](https://discourse.julialang.org/t/best-practices/85138/6 "2022-08-02T09:28:51Z")

</div>

> [@gustaphe](#):
>
> `for (i, item) in enumerate(vec)`

Or  
`for (i, item) in pairs(vec)`  
if you want the indice in the vec with the item

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [August 2, 2022, 9:57am UTC](https://discourse.julialang.org/t/best-practices/85138/7 "2022-08-02T09:57:38Z")

</div>

> [@jdad](#):
>
> Or

I agree, but, more emphatically: `enumerate` is the wrong function, `pairs` is the right one, but people always forget it.

Edit: Ah, well, @gustaphe did say ‘counter’, which is alright. I just have a knee-jerk reaction to all the uses of `enumerate`.

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [August 2, 2022, 10:49am UTC](https://discourse.julialang.org/t/best-practices/85138/8 "2022-08-02T10:49:06Z")

</div>

`eachindex` usually improves performance anyways because it uses cartesian directly.

---

<div class="post-metadata">

**Author:** ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)\
**Post date:** [August 2, 2022, 11:05am UTC](https://discourse.julialang.org/t/best-practices/85138/9 "2022-08-02T11:05:58Z")

</div>

Yeah, I see it the other way, I normally _either_ need the indices _or_ just the items, if I need something together with the items it’s probably a counter.

(Plus, `pairs` is so obscure a name I didn’t dare recommending it without double checking)

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [August 2, 2022, 11:15am UTC](https://discourse.julialang.org/t/best-practices/85138/10 "2022-08-02T11:15:02Z")

</div>

> [@gustaphe](#):
>
> Yeah, I see it the other way, I normally _either_ need the indices _or_ just the items, if I need something together with the items it’s probably a counter.

Oh, that is very different from my experience. I think it’s probably 80-20 that I need `pairs`.

> [@gustaphe](#):
>
> (Plus, `pairs` is so obscure a name I didn’t dare recommending it without double checking)

Yeah, that’s what I think is a bit sad. Particularly because I very often see `enumerate` being used to get indices in the wild.

---

<div class="post-metadata">

**Author:** ![jdad](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jdad/32/4739_2.png) [@jdad](https://discourse.julialang.org/u/jdad)\
**Post date:** [August 2, 2022, 11:37am UTC](https://discourse.julialang.org/t/best-practices/85138/11 "2022-08-02T11:37:39Z")

</div>

> [@gustaphe](#):
>
> Plus, `pairs` is so obscure a name I didn’t dare recommending it without double checking

I would say (as a mnemotechnic) that `pairs` is for pairs of `(key, value)` , here `(index,value)`, while, clearly, `enumerate` goes from `one` to the `end`, so is for a counter.

By the way, for `enumerate` it is easy to test for the first iteration `( i == 1)`, but how to test with last iteration ? ( does `( i == end)` works? I do not see how it could). Possibly it is no a real worry, because less frequently interesting that test for first iter, but it arises sometime nevertheless.

On the other side with `pairs` you can do `( i == firstindex(v) ) ` and `( i == lastindex(v) )`, even if it is somewhat verbose?

Comment or suggestions welcome.

---

<div class="post-metadata">

**Author:** ![Paul\_Soderlind](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paul_soderlind/32/1753_2.png) [@Paul\_Soderlind](https://discourse.julialang.org/u/Paul_Soderlind)\
**Post date:** [August 2, 2022, 1:00pm UTC](https://discourse.julialang.org/t/best-practices/85138/12 "2022-08-02T13:00:18Z")

</div>

If `vec` is meant to a traditional (1-indexed) vector, then the “generality” argument should be weighed against the simplicity/portability of `length()`.

I believe this trade-off is a bit different for package developers and end-users (especially newcomers to Julia).

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [August 2, 2022, 1:28pm UTC](https://discourse.julialang.org/t/best-practices/85138/13 "2022-08-02T13:28:24Z")

</div>

> [@Paul\_Soderlind](#):
>
> the simplicity/portability of `length()`.

Not sure I understand. Is `1:length()` more portable?

As for simplicity, `eachindex()` is simpler than `1:length()`, but perhaps less familiar to newcomers. But, once you’ve heard of it, you know it.

---

<div class="post-metadata">

**Author:** ![Paul\_Soderlind](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paul_soderlind/32/1753_2.png) [@Paul\_Soderlind](https://discourse.julialang.org/u/Paul_Soderlind)\
**Post date:** [August 2, 2022, 2:11pm UTC](https://discourse.julialang.org/t/best-practices/85138/14 "2022-08-02T14:11:38Z")

</div>

> Is `1:length()` more portable?

Well, in the sense that it looks pretty _similar_ to what you would do in some of Julia’s competitors.

---

<div class="post-metadata">

**Author:** ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)\
**Post date:** [August 2, 2022, 2:46pm UTC](https://discourse.julialang.org/t/best-practices/85138/15 "2022-08-02T14:46:14Z")

</div>

Absolutely, but there are plenty of reasonable things `pairs(vec)` could have meant. I don’t know what I would have wanted it to be called, I just know that I never feel entire certain what `pairs` does (or more likely what it’s called).

```julia
i == length(vec) && return

```

---

<div class="post-metadata">

**Author:** ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)\
**Post date:** [August 2, 2022, 2:48pm UTC](https://discourse.julialang.org/t/best-practices/85138/16 "2022-08-02T14:48:41Z")

</div>

That’s a feature. Less risk of off-by-one if you’re translating from `eachindex` than from `1:length`.

---

<div class="post-metadata">

**Author:** ![Paul\_Soderlind](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paul_soderlind/32/1753_2.png) [@Paul\_Soderlind](https://discourse.julialang.org/u/Paul_Soderlind)\
**Post date:** [August 2, 2022, 6:28pm UTC](https://discourse.julialang.org/t/best-practices/85138/17 "2022-08-02T18:28:18Z")

</div>

`eachindex` is fine and even `for (i, item) in enumerate(vec)` is something that most of us can handle (although it isn’t pretty).  
The real problem starts when dealing with matrices (and more). Things like `for t = 4:T;i =2:N;x[t,i] = x[t-3,i-1];...` are very easy in traditional notation, but become rather involved when you want to handle OffsetArrays (and what not). I believe this might become a serious hurdle in trying to attract new users.

---

<div class="post-metadata">

**Author:** ![Dario-Rosa85](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dario-rosa85/32/32358_2.png) [@Dario-Rosa85](https://discourse.julialang.org/u/Dario-Rosa85)\
**Post date:** [August 4, 2022, 4:38am UTC](https://discourse.julialang.org/t/best-practices/85138/18 "2022-08-04T04:38:47Z")

</div>

Thanks all for the discussion!

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [August 4, 2022, 4:40am UTC](https://discourse.julialang.org/t/best-practices/85138/19 "2022-08-04T04:40:14Z")

</div>

> [@Paul\_Soderlind](#):
>
> `for t = 4:T;i =2:N;x[t,i] = x[t-3,i-1];`

```julia
for c in CartesianIndices(M)

```

?

btw, `enumerate` doesn’t work with OffsetArray because `i` starts at `1`

---

<div class="post-metadata">

**Author:** ![Paul\_Soderlind](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paul_soderlind/32/1753_2.png) [@Paul\_Soderlind](https://discourse.julialang.org/u/Paul_Soderlind)\
**Post date:** [August 4, 2022, 7:01am UTC](https://discourse.julialang.org/t/best-practices/85138/20 "2022-08-04T07:01:47Z")

</div>

in this case, perhaps this:

> [@Is OffsetArrays.jl a poison pill?](https://discourse.julialang.org/t/is-offsetarrays-jl-a-poison-pill/85188/43):
>
> Or alternately, for t in axes(x,1)[begin+3:end], for i in axes(x,2)[begin+1:end] The axes may be indexed as arrays as well.

[Next page](https://discourse.julialang.org/t/best-practices/85138.md?page=2)
