# Alternative to very time-consuming array operations

**URL:** <https://discourse.julialang.org/t/alternative-to-very-time-consuming-array-operations/70620>\
**Category:** Performance\
**Tags:** question\
**Created:** [October 29, 2021, 12:47pm UTC](https://discourse.julialang.org/t/alternative-to-very-time-consuming-array-operations/70620 "2021-10-29T12:47:21Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![anneJulia](https://avatars.discourse-cdn.com/v4/letter/a/f4b2a3/32.png) [@anneJulia](https://discourse.julialang.org/u/anneJulia)\
**Post date:** [October 29, 2021, 12:47pm UTC](https://discourse.julialang.org/t/alternative-to-very-time-consuming-array-operations/70620/1 "2021-10-29T12:47:21Z")

</div>

I am writing a function where array operations, such as the example below, have to happen a few hundred thousand times.

```julia
arr = [1,0,0,1,1]
idx = [2,4]

res = arr[idx]

```

When using a profiler to analyse the code 89.69% of the time is spent calling getindex. Is there an alternative to using arrays, or some way to improve runtime?

---

<div class="post-metadata">

**Author:** ![gbaraldi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gbaraldi/32/22101_2.png) [@gbaraldi](https://discourse.julialang.org/u/gbaraldi)\
**Post date:** [October 29, 2021, 12:53pm UTC](https://discourse.julialang.org/t/alternative-to-very-time-consuming-array-operations/70620/2 "2021-10-29T12:53:27Z")

</div>

How large are the arrays? You could use something like [https://github.com/JuliaArrays/StaticArrays.jl](https://github.com/JuliaArrays/StaticArrays.jl) if they are small (under 20 elements). If you don’t mutate your results you could use views too, so you don’t allocate a new array every time.  
Reusing the aray could also help if it is possible.

---

<div class="post-metadata">

**Author:** ![non-Jedi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/non-jedi/32/3645_2.png) [@non-Jedi](https://discourse.julialang.org/u/non-Jedi)\
**Post date:** [October 29, 2021, 12:55pm UTC](https://discourse.julialang.org/t/alternative-to-very-time-consuming-array-operations/70620/3 "2021-10-29T12:55:42Z")

</div>

Please take a look at the docstring for the `@inbounds` macro. Things like `idx` in your example are probably better as tuples. If your data all binary, you probably want an array of `Bool` rather than `Int64`. Also probably worthwhile to give the “Performance Tips” page of the Julia manual another read-through.

---

<div class="post-metadata">

**Author:** ![anneJulia](https://avatars.discourse-cdn.com/v4/letter/a/f4b2a3/32.png) [@anneJulia](https://discourse.julialang.org/u/anneJulia)\
**Post date:** [October 29, 2021, 1:17pm UTC](https://discourse.julialang.org/t/alternative-to-very-time-consuming-array-operations/70620/4 "2021-10-29T13:17:07Z")

</div>

I have already used @inbounds and also started Julia with --check-bounds=no and still, getindex is the most time-consuming.

---

<div class="post-metadata">

**Author:** ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)\
**Post date:** [October 29, 2021, 1:27pm UTC](https://discourse.julialang.org/t/alternative-to-very-time-consuming-array-operations/70620/5 "2021-10-29T13:27:19Z")

</div>

As @gbaraldi already said, if your actual code uses small arrays or vectors like your MWE does, you need to be using StaticArrays for this. The speedup is unreal (yet very very real) – sort of surreal (really!)

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [October 29, 2021, 1:35pm UTC](https://discourse.julialang.org/t/alternative-to-very-time-consuming-array-operations/70620/6 "2021-10-29T13:35:54Z")

</div>

You could maybe use a view, `res = @view arr[idx]`. Without more context it is impossible to say. Doing `arr[idx]` a bunch of times will take time since you need to allocate a new array every time, so rewrite the code not to do that.

---

<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:** [October 29, 2021, 3:19pm UTC](https://discourse.julialang.org/t/alternative-to-very-time-consuming-array-operations/70620/7 "2021-10-29T15:19:24Z")

</div>

> [@gbaraldi](#):
>
> if they are small (under 20 elements).

I think the rule of thumb is up to 100 elements, or it used to be.

This definitely looks like a job for StaticArrays, or possibly tuples.

---

<div class="post-metadata">

**Author:** ![anneJulia](https://avatars.discourse-cdn.com/v4/letter/a/f4b2a3/32.png) [@anneJulia](https://discourse.julialang.org/u/anneJulia)\
**Post date:** [November 4, 2021, 8:18am UTC](https://discourse.julialang.org/t/alternative-to-very-time-consuming-array-operations/70620/8 "2021-11-04T08:18:24Z")

</div>

I tried using the static arrays but compared to the normal arrays, the computation time went up. This is probably since I used nested static arrays ([Alternative to very time-consuming array operations](https://discourse.julialang.org/t/alternative-to-very-time-consuming-array-operations/70620)).

---

<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:** [November 4, 2021, 8:42am UTC](https://discourse.julialang.org/t/alternative-to-very-time-consuming-array-operations/70620/9 "2021-11-04T08:42:22Z")

</div>

Can you share an MWE?

---

<div class="post-metadata">

**Author:** ![anneJulia](https://avatars.discourse-cdn.com/v4/letter/a/f4b2a3/32.png) [@anneJulia](https://discourse.julialang.org/u/anneJulia)\
**Post date:** [November 4, 2021, 8:44am UTC](https://discourse.julialang.org/t/alternative-to-very-time-consuming-array-operations/70620/10 "2021-11-04T08:44:24Z")

</div>

I posted one in the new topic: [Nested static arrays perform worse than nested arrays](https://discourse.julialang.org/t/nested-static-arrays-perform-worse-than-nested-arrays/70938)

(sorry I just saw that I attached the wrong link)
