# How to access all field values for one field of a composite type array

**URL:** <https://discourse.julialang.org/t/how-to-access-all-field-values-for-one-field-of-a-composite-type-array/8287>\
**Category:** General Usage\
**Tags:** question\
**Created:** [January 11, 2018, 11:31am UTC](https://discourse.julialang.org/t/how-to-access-all-field-values-for-one-field-of-a-composite-type-array/8287 "2018-01-11T11:31:43Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![LotteVictor](https://avatars.discourse-cdn.com/v4/letter/l/b19c9b/32.png) [@LotteVictor](https://discourse.julialang.org/u/LotteVictor)\
**Post date:** [January 11, 2018, 11:31am UTC](https://discourse.julialang.org/t/how-to-access-all-field-values-for-one-field-of-a-composite-type-array/8287/1 "2018-01-11T11:31:43Z")

</div>

Hi there! I’ve created a composite type with:

> mutable struct TAdult  
> id::Int64  
> strain::String  
> hind::Float64  
> gind::Float64  
> tradeoff::Float64  
> end

Then I create an array of that type with:

> function InitAdults(ft::Int64, fhind\_mean::Float64, fhind\_std::Float64, fgind\_mean::Float64, fgind\_std::Float64, tcost::Float64)  
> Adults=Array{TAdult}(0)  
> for i in 1:ft  
> id=i  
> strain=string(id)  
> hind=rand(Normal(fhind\_mean,fhind\_std))  
> gind=rand(LogNormal(fgind\_mean,fgind\_std))  
> tradeoff=exp((-gind^2)/(2\*tcost^2))  
> nAdult=TAdult(id, strain, hind, gind, tradeoff)  
> push!(Adults, nAdult)  
> end  
> return Adults  
> end

In a new function, I need to access certain fields of all TAdult in that array. I can achive this by looping over the array (e.g. in a function)

> function idvector(fAdults::Array{TAdult,1})  
> idvector=Float64  
> for i in 1:length(fAdults)  
> push!(idvector, fAdults[i].id)  
> end  
> return idvector  
> end

However, I think this takes too long and I want to improve my performance. So I was wondering whether there was a possible way to do something similar to  
`idvector= Adults[1:end].id`  
I thought about using a tuple type, but if I understand it correctly, tuple types are immutable? And also would only be accessible by index, not fieldname. Does anybody have an idea how to solve this?  
Thanks alot!

---

<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:** [January 11, 2018, 11:48am UTC](https://discourse.julialang.org/t/how-to-access-all-field-values-for-one-field-of-a-composite-type-array/8287/2 "2018-01-11T11:48:47Z")

</div>

Did you measure and found out that the loop was slow? It looks perfectly fine to me and any alternative way of writing this will, in the end, have to do the same as what you have written, loop over all elements and extract the `id` field.

---

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [January 11, 2018, 12:05pm UTC](https://discourse.julialang.org/t/how-to-access-all-field-values-for-one-field-of-a-composite-type-array/8287/3 "2018-01-11T12:05:30Z")

</div>

Try a comprehension.

```julia
idvector = [x.id for x in Adults]

```

---

<div class="post-metadata">

**Author:** ![LotteVictor](https://avatars.discourse-cdn.com/v4/letter/l/b19c9b/32.png) [@LotteVictor](https://discourse.julialang.org/u/LotteVictor)\
**Post date:** [January 11, 2018, 2:02pm UTC](https://discourse.julialang.org/t/how-to-access-all-field-values-for-one-field-of-a-composite-type-array/8287/4 "2018-01-11T14:02:26Z")

</div>

The loop itself isn’t slow, but I need to access different fields of several thousand instances repeteadly and my whole simulation is going to take ten hours if I do not find a faster way. So there is definetly the need for performance improvement. I was just wondering, whether there is a fast track to access an indexed value of an indexed array in an array…Apperently there is none (?), so I’ll probably have to change the simulation at a more basal level. Thanks for your input! 🙂

---

<div class="post-metadata">

**Author:** ![LotteVictor](https://avatars.discourse-cdn.com/v4/letter/l/b19c9b/32.png) [@LotteVictor](https://discourse.julialang.org/u/LotteVictor)\
**Post date:** [January 11, 2018, 2:03pm UTC](https://discourse.julialang.org/t/how-to-access-all-field-values-for-one-field-of-a-composite-type-array/8287/5 "2018-01-11T14:03:19Z")

</div>

@GunnarFarneback I’ll try this and see if it’s faster. Thank you 🙂

---

<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:** [January 11, 2018, 2:04pm UTC](https://discourse.julialang.org/t/how-to-access-all-field-values-for-one-field-of-a-composite-type-array/8287/6 "2018-01-11T14:04:58Z")

</div>

Perhaps using a “Struct of Arrays” would work better e.g:

```julia
struct TAdults
    ids::Vector{Int64}
    strains::Vector{String}
    hinds::Vector{Float64}
    ginds::Vector{Float64}
    tradeoffs::Vector{Float64}
end
```

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [January 11, 2018, 2:06pm UTC](https://discourse.julialang.org/t/how-to-access-all-field-values-for-one-field-of-a-composite-type-array/8287/7 "2018-01-11T14:06:13Z")

</div>

> [@LotteVictor](#):
>
> access different fields of several thousand instances repeteadly and my whole simulation is going to take ten hours if I do not find a faster way

Did you in fact profile your code and ascertain that this is the bottleneck? When the types are known to the compiler, `value.field` accessors are very fast.

---

<div class="post-metadata">

**Author:** ![LotteVictor](https://avatars.discourse-cdn.com/v4/letter/l/b19c9b/32.png) [@LotteVictor](https://discourse.julialang.org/u/LotteVictor)\
**Post date:** [January 11, 2018, 2:08pm UTC](https://discourse.julialang.org/t/how-to-access-all-field-values-for-one-field-of-a-composite-type-array/8287/8 "2018-01-11T14:08:31Z")

</div>

That’s a good idea, but would not represent what I want to achieve. Every instance of TAdult needs to accessed later on again, replicated and even changed sometimes.

---

<div class="post-metadata">

**Author:** ![LotteVictor](https://avatars.discourse-cdn.com/v4/letter/l/b19c9b/32.png) [@LotteVictor](https://discourse.julialang.org/u/LotteVictor)\
**Post date:** [January 11, 2018, 2:13pm UTC](https://discourse.julialang.org/t/how-to-access-all-field-values-for-one-field-of-a-composite-type-array/8287/9 "2018-01-11T14:13:12Z")

</div>

No I didn’t find the actual bottleneck yet.  
But I have an idea, about what could take so long (which is not the access to the field, but changing it later on), which I thought I could shorten up with a different way to access the fields. However, I’m rather new to all this (especially performance improvement hasn’t been an issue yet), but before I expand my simulation, I want to make sure, it runs as fast as possible… It’s good to know that `value.field` is usually fast… Thank you so much!

---

<div class="post-metadata">

**Author:** ![sdanisch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sdanisch/32/1406_2.png) [@sdanisch](https://discourse.julialang.org/u/sdanisch)\
**Post date:** [January 11, 2018, 2:13pm UTC](https://discourse.julialang.org/t/how-to-access-all-field-values-for-one-field-of-a-composite-type-array/8287/10 "2018-01-11T14:13:56Z")

</div>

You can automate that process with: [https://github.com/simonster/StructsOfArrays.jl](https://github.com/simonster/StructsOfArrays.jl)

Btw, iterating Vector{\<: mutable struct} is quite a bit slower then Vector{\<:struct}, since you end up iterating a linked list. So trying out the struct approach and if it’s lots faster figure out how to work with the immutability could be the best way to deal with this.

---

<div class="post-metadata">

**Author:** ![LotteVictor](https://avatars.discourse-cdn.com/v4/letter/l/b19c9b/32.png) [@LotteVictor](https://discourse.julialang.org/u/LotteVictor)\
**Post date:** [January 11, 2018, 2:20pm UTC](https://discourse.julialang.org/t/how-to-access-all-field-values-for-one-field-of-a-composite-type-array/8287/11 "2018-01-11T14:20:20Z")

</div>

Thanks! I know that it would be faster to have immutable struct, but I think that’s not really a possibility here. I need to be able to change everything rather often (with a probability of 0.03% of ~10 000 \*100 instances). Or do you have a good idea how I could deal with immutablity in this case?  
I’ll have a look into StructsOfArrays. This sounds interesting. Thank you!

---

<div class="post-metadata">

**Author:** ![LotteVictor](https://avatars.discourse-cdn.com/v4/letter/l/b19c9b/32.png) [@LotteVictor](https://discourse.julialang.org/u/LotteVictor)\
**Post date:** [January 11, 2018, 2:23pm UTC](https://discourse.julialang.org/t/how-to-access-all-field-values-for-one-field-of-a-composite-type-array/8287/12 "2018-01-11T14:23:30Z")

</div>

So far, this didn’t improve the performance. If I call the above mentioned function I get:

> @time fooidvector(Testpop)  
> 0.000002 seconds (8 allocations: 512 bytes)

and if I use the comprehension it’s

> @time idvector = [x.id for x in Testpop]  
> 0.039866 seconds (8.72 k allocations: 488.114 KiB)

Is this something you’d expect? Thanks alot!

---

<div class="post-metadata">

**Author:** ![sdanisch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sdanisch/32/1406_2.png) [@sdanisch](https://discourse.julialang.org/u/sdanisch)\
**Post date:** [January 11, 2018, 2:25pm UTC](https://discourse.julialang.org/t/how-to-access-all-field-values-for-one-field-of-a-composite-type-array/8287/13 "2018-01-11T14:25:03Z")

</div>

The compiler is very good at optimizing immutables. Try it out and benchmark 😉 If the approach works, you might want to have some helpers to “modify” the immutables. Hopefully, this should be a lot nicer on 0.7!

---

<div class="post-metadata">

**Author:** ![LotteVictor](https://avatars.discourse-cdn.com/v4/letter/l/b19c9b/32.png) [@LotteVictor](https://discourse.julialang.org/u/LotteVictor)\
**Post date:** [January 11, 2018, 2:30pm UTC](https://discourse.julialang.org/t/how-to-access-all-field-values-for-one-field-of-a-composite-type-array/8287/14 "2018-01-11T14:30:48Z")

</div>

Thanks a lot. I’ll try to figure out if there is a way for me to use immutables. with helpers, do you mean find/create functions to change the values? I need them permanently changed (because of evolution 😉 ) and can’t have them show up changed for certain parts of the simulation and then not be changed in the initial instance.  
Or did you already have something in mind to help achieve this? Thank you so much!

---

<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:** [January 11, 2018, 2:45pm UTC](https://discourse.julialang.org/t/how-to-access-all-field-values-for-one-field-of-a-composite-type-array/8287/15 "2018-01-11T14:45:28Z")

</div>

> [@sdanisch](#):
>
> Btw, iterating Vector{\<: mutable struct} is quite a bit slower then Vector{\<:struct}, since you end up iterating a linked list.

Iterating an `isbits` is faster. Even with `immutable` this wouldn’t be `isbits` due to the string field.

> [@LotteVictor](#):
>
> No I didn’t find the actual bottleneck yet.

This is the first thing to do though. How can you try optimize code if you don’t know what is slow?

---

<div class="post-metadata">

**Author:** ![sdanisch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sdanisch/32/1406_2.png) [@sdanisch](https://discourse.julialang.org/u/sdanisch)\
**Post date:** [January 11, 2018, 2:47pm UTC](https://discourse.julialang.org/t/how-to-access-all-field-values-for-one-field-of-a-composite-type-array/8287/16 "2018-01-11T14:47:23Z")

</div>

I would have thought the array is the shared state 😉  
It won’t work if you pass around the instances outside the array and mutate them at multiple stages and expect everything to update.

---

<div class="post-metadata">

**Author:** ![sdanisch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sdanisch/32/1406_2.png) [@sdanisch](https://discourse.julialang.org/u/sdanisch)\
**Post date:** [January 11, 2018, 2:48pm UTC](https://discourse.julialang.org/t/how-to-access-all-field-values-for-one-field-of-a-composite-type-array/8287/17 "2018-01-11T14:48:14Z")

</div>

> Iterating an isbits is faster. Even with immutable this wouldn’t be isbits due to the string field.

Good point, I overlooked the String… Would be interesting if that really needs to be a string though

---

<div class="post-metadata">

**Author:** ![LotteVictor](https://avatars.discourse-cdn.com/v4/letter/l/b19c9b/32.png) [@LotteVictor](https://discourse.julialang.org/u/LotteVictor)\
**Post date:** [January 11, 2018, 2:56pm UTC](https://discourse.julialang.org/t/how-to-access-all-field-values-for-one-field-of-a-composite-type-array/8287/18 "2018-01-11T14:56:57Z")

</div>

I think, the string rellay need to be a string. It contains a mixture of characters and numbers and can be rather long (like 1h23m45h68h79m50 or something similar). Is there a possibility to do this without a string?

---

<div class="post-metadata">

**Author:** ![LotteVictor](https://avatars.discourse-cdn.com/v4/letter/l/b19c9b/32.png) [@LotteVictor](https://discourse.julialang.org/u/LotteVictor)\
**Post date:** [January 11, 2018, 2:58pm UTC](https://discourse.julialang.org/t/how-to-access-all-field-values-for-one-field-of-a-composite-type-array/8287/19 "2018-01-11T14:58:51Z")

</div>

True, I should have done that first. Do you have any tips on doing this correctly? Or a nice link? There is the BenchmarkTools.jl package, for benchmarking. What exactly did you mean before with profiling? Can you recommend a homepage to read? Thanks again!

---

<div class="post-metadata">

**Author:** ![sdanisch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sdanisch/32/1406_2.png) [@sdanisch](https://discourse.julialang.org/u/sdanisch)\
**Post date:** [January 11, 2018, 3:01pm UTC](https://discourse.julialang.org/t/how-to-access-all-field-values-for-one-field-of-a-composite-type-array/8287/20 "2018-01-11T15:01:49Z")

</div>

> [@LotteVictor](#):
>
> 1h23m45h68h79m50

Well, @kristoffer.carlsson is right, you should first find your actual bottleneck, before you dive into further optimizations 😉  
But if the string is fixedsize you could use something like NTuple{N, UInt8} or NTuple{N, Char} or [FixedSizeStrings.jl](https://github.com/JuliaComputing/FixedSizeStrings.jl/issues).  
Or just save an index into a string array - but that will just get you down further the premature optimization rabbit hole, and you should really first pin down the actual bottlenecks 🙂

About the profiling: [https://docs.julialang.org/en/stable/manual/profile/](https://docs.julialang.org/en/stable/manual/profile/)

[Next page](https://discourse.julialang.org/t/how-to-access-all-field-values-for-one-field-of-a-composite-type-array/8287.md?page=2)
