# Assigning a Tuple to sub-entries of Tuple of Arrays

**URL:** <https://discourse.julialang.org/t/assigning-a-tuple-to-sub-entries-of-tuple-of-arrays/30871>\
**Category:** New to Julia\
**Tags:** performance, array, tuple\
**Created:** [November 8, 2019, 7:12pm UTC](https://discourse.julialang.org/t/assigning-a-tuple-to-sub-entries-of-tuple-of-arrays/30871 "2019-11-08T19:12:33Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jlchan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jlchan/32/10958_2.png) [@jlchan](https://discourse.julialang.org/u/jlchan)\
**Post date:** [November 8, 2019, 7:12pm UTC](https://discourse.julialang.org/t/assigning-a-tuple-to-sub-entries-of-tuple-of-arrays/30871/1 "2019-11-08T19:12:33Z")

</div>

I’m distributing entries of a Tuple to the ith entry of each array in a Tuple of Arrays.

```julia
using BenchmarkTools

N = 3
X = (zeros(N),zeros(N))
Y = [1,2]
function foo1!(X,Y,i)
    for fld in eachindex(X)
        X[fld][i] = Y[fld]
    end
end
@btime foo1!($X,$Y,$1)

foo2!(x,y,i) = x[i] = y
@btime foo2!.($X,$Y,$1)

foo3!(X,Y,i) = getindex(X,i) = Y
@btime foo3!($X,$Y,$1)

```

Each different approach gives a different runtimes

```julia
  2.608 ns (0 allocations: 0 bytes)
  44.127 ns (1 allocation: 96 bytes)
  5.209 ns (1 allocation: 16 bytes)

```

Why is the second so slow, and is there a one-line approach which is as fast as the for loop?

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [November 8, 2019, 7:16pm UTC](https://discourse.julialang.org/t/assigning-a-tuple-to-sub-entries-of-tuple-of-arrays/30871/2 "2019-11-08T19:16:16Z")

</div>

The second also returns an array of results. You might be interested in

> **[GitHub - JuliaArrays/StructArrays.jl: Efficient implementation of struct...](https://github.com/JuliaArrays/StructArrays.jl)**
>
> Efficient implementation of struct arrays in Julia - GitHub - JuliaArrays/StructArrays.jl: Efficient implementation of struct arrays in Julia

---

<div class="post-metadata">

**Author:** ![jlchan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jlchan/32/10958_2.png) [@jlchan](https://discourse.julialang.org/u/jlchan)\
**Post date:** [November 8, 2019, 8:42pm UTC](https://discourse.julialang.org/t/assigning-a-tuple-to-sub-entries-of-tuple-of-arrays/30871/3 "2019-11-08T20:42:57Z")

</div>

Thanks! I didn’t account for the allocation of return values.
