# Most efficient way to unpack array into variables?

**URL:** <https://discourse.julialang.org/t/most-efficient-way-to-unpack-array-into-variables/13725>\
**Category:** General Usage\
**Tags:** question, performance\
**Created:** [August 19, 2018, 6:47pm UTC](https://discourse.julialang.org/t/most-efficient-way-to-unpack-array-into-variables/13725 "2018-08-19T18:47:26Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![anon32405968](https://avatars.discourse-cdn.com/v4/letter/a/57b2e6/32.png) [@anon32405968](https://discourse.julialang.org/u/anon32405968)\
**Post date:** [August 19, 2018, 6:47pm UTC](https://discourse.julialang.org/t/most-efficient-way-to-unpack-array-into-variables/13725/1 "2018-08-19T18:47:26Z")

</div>

When optimising some complicated model, I find two things are important:

1. Readability (I want to use correct variable names, not `array_of_parameters[index_required]`)

2. Speed (the function may be called many thousands of times)

The model function parameters argument needs to be a vector (in NLopt.jl which I am currently using in my package).

A generalized example of what I currently do is the following:

`var1, var2, var3 = array_of_params`

But I am wondering if there are faster ways of doing this? Or if there is just a known best solution that I have missed?

I have read the following:

[https://groups.google.com/forum/#!topic/julia-users/IQS2mT1ITwU](https://groups.google.com/forum/#!topic/julia-users/IQS2mT1ITwU)

> [@Macro to automatically create large number of variables](https://discourse.julialang.org/t/macro-to-automatically-create-large-number-of-variables/13150):
>
> Hello everyone, I have a model where I need to generate a large number of variables of the form r = exp.(Xss[indexes.rSS] .+ X[indexes.r]) rPrime = exp.(Xss[indexes.rSS] .+ XPrime[indexes.r]) w = exp.(Xss[indexes.wSS] .+ X[indexes.w]) wPrime = exp.(Xss[indexes.wSS] .+ XPrime[indexes.w]) within a function and and I’d like to use metaprogramming to not have to do this manually. At the global scope, I can easily do this with eval() # Preliminary setup struct index…

But it’s still not clear to me how to do this with minimum overhead. Any suggestions would be greatly appreciated!

---

<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 19, 2018, 7:47pm UTC](https://discourse.julialang.org/t/most-efficient-way-to-unpack-array-into-variables/13725/2 "2018-08-19T19:47:15Z")

</div>

```julia
julia> arr = [1,2,3]
julia> @btime a,b,c = $arr
  2.027 ns (0 allocations: 0 bytes)
3-element Array{Int64,1}:
 1
 2
 3

```

Your current solution seems really fast already. Do you really think that it will be a bottleneck?

As for the question you linked to, that concerns programatically generating variable identifiers, which I think is a bad idea that makes code _less_ readable, not more.

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [August 19, 2018, 8:23pm UTC](https://discourse.julialang.org/t/most-efficient-way-to-unpack-array-into-variables/13725/3 "2018-08-19T20:23:13Z")

</div>

How many variables does your code have, roughly? And is every variable a scalar, or is there more structure to your data? There may be a few different options depending on the particulars of your problem.

---

<div class="post-metadata">

**Author:** ![anon32405968](https://avatars.discourse-cdn.com/v4/letter/a/57b2e6/32.png) [@anon32405968](https://discourse.julialang.org/u/anon32405968)\
**Post date:** [August 19, 2018, 11:00pm UTC](https://discourse.julialang.org/t/most-efficient-way-to-unpack-array-into-variables/13725/4 "2018-08-19T23:00:55Z")

</div>

At present it varies from 2 - 6. Larger models might be included in future which have more parameters (~10 to 20). Every variable is a scalar, usually Float64.

Looking @DNF’s benchmark, I wonder if I’m spending energy optimizing in the wrong place in this instance. However, I’m still very interested for future reference as it will definitely be useful to know for the type of work I’m doing.

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [August 20, 2018, 2:30am UTC](https://discourse.julialang.org/t/most-efficient-way-to-unpack-array-into-variables/13725/5 "2018-08-20T02:30:48Z")

</div>

Maybe something like [GitHub - rafaqz/Flatten.jl: Flatten nested Julia objects to tuples, and reconstruct them later](https://github.com/rafaqz/Flatten.jl) would be helpful in your case? It makes it easier to unpack a vector, not into a list of variables but into a structure with named fields.

---

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [August 20, 2018, 2:39am UTC](https://discourse.julialang.org/t/most-efficient-way-to-unpack-array-into-variables/13725/6 "2018-08-20T02:39:43Z")

</div>

The style you adopted, direct assignment from a single sequentially ordered datatype into distinct, appropriately named, variables is **the win** : it combines obviousness, clarity for others, and performance.

The current internal setting for the number of args a function may process quickly, without falling back on other machinery, has been increased from ~14 to ~30 or more. Your explicit assignments are unlikely to find that sort of limit.

---

<div class="post-metadata">

**Author:** ![anon32405968](https://avatars.discourse-cdn.com/v4/letter/a/57b2e6/32.png) [@anon32405968](https://discourse.julialang.org/u/anon32405968)\
**Post date:** [August 24, 2018, 10:54am UTC](https://discourse.julialang.org/t/most-efficient-way-to-unpack-array-into-variables/13725/7 "2018-08-24T10:54:19Z")

</div>

Thanks that makes sense.

If possible, could you post any links to other discourse posts / github issues / documentation that you think would help me understand what’s going on ‘under the hood’ that introduces this ~30 args limit?

---

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [August 24, 2018, 2:42pm UTC](https://discourse.julialang.org/t/most-efficient-way-to-unpack-array-into-variables/13725/8 "2018-08-24T14:42:11Z")

</div>

Its in the source code and not something you want to alter. I don’t recall which, there is some github issue.
