# Efficiently looping over structs in Julia

**URL:** https://discourse.julialang.org/t/efficiently-looping-over-structs-in-julia/52217
**Category:** New to Julia
**Created:** [December 22, 2020, 5:32am UTC](https://discourse.julialang.org/t/efficiently-looping-over-structs-in-julia/52217 "2020-12-22T05:32:15Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![manuka](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/manuka/32/20453_2.png) [@manuka](https://discourse.julialang.org/u/manuka)
#### Post date: [December 22, 2020, 5:32am UTC](https://discourse.julialang.org/t/efficiently-looping-over-structs-in-julia/52217/1 "2020-12-22T05:32:15Z")

</div>

Hello,

I have a dire question with regards to memory allocation of structs. I have posted it in Stackoverflow; please answer it in Stackoverflow:

[https://stackoverflow.com/q/65403410/14869451](https://stackoverflow.com/q/65403410/14869451)

Many thanks.

---

<div class="post-metadata">

### Author: ![lungben](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lungben/32/12314_2.png) [@lungben](https://discourse.julialang.org/u/lungben)
#### Post date: [December 22, 2020, 7:20am UTC](https://discourse.julialang.org/t/efficiently-looping-over-structs-in-julia/52217/2 "2020-12-22T07:20:47Z")

</div>

There are already helpful answers by @Oscar_Smith posted in SO.

---

<div class="post-metadata">

### Author: ![hendri54](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hendri54/32/9621_2.png) [@hendri54](https://discourse.julialang.org/u/hendri54)
#### Post date: [December 22, 2020, 12:20pm UTC](https://discourse.julialang.org/t/efficiently-looping-over-structs-in-julia/52217/3 "2020-12-22T12:20:16Z")

</div>

If you want the struct to be mutable, why not reuse it in each loop?

```julia
using Distributions, BenchmarkTools

mutable struct help_me{Z}
    can_you_help_me::Z
    millions_of_thanks::Z
end

function set_help!(h, x, y)
    h.can_you_help_me = x;
    h.millions_of_thanks = y;
end

function runner()
    tmp_help = help_me(rand(Bernoulli(0.5)), rand(Bernoulli(0.99)));
    max_iter = 10_000;
    for i in 1:max_iter
        set_help!(tmp_help, rand(Bernoulli(0.5)), rand(Bernoulli(0.99)));
        # many follow-up processes 
    end
end

@btime runner()
  116.107 μs (0 allocations: 0 bytes)

```

---

<div class="post-metadata">

### Author: ![manuka](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/manuka/32/20453_2.png) [@manuka](https://discourse.julialang.org/u/manuka)
#### Post date: [December 22, 2020, 6:55pm UTC](https://discourse.julialang.org/t/efficiently-looping-over-structs-in-julia/52217/4 "2020-12-22T18:55:50Z")

</div>

Thanks a lot!

---

<div class="post-metadata">

### Author: ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)
#### Post date: [December 22, 2020, 7:43pm UTC](https://discourse.julialang.org/t/efficiently-looping-over-structs-in-julia/52217/5 "2020-12-22T19:43:06Z")

</div>

Just for if it was not clear from the answer in the Stack Overflow, the allocations were not associated to the loop initializing the struct. This is fast and does not allocate either:

```julia
struct help_me2{Z}
    can_you_help_me::Z
    millions_of_thanks::Z
end

function runner2()
    tmp_help = help_me2(rand(Bernoulli(0.5)), rand(Bernoulli(0.99)));
    max_iter = 10_000;
    for i in 1:max_iter
        tmp_help = help_me2(rand(Bernoulli(0.5)), rand(Bernoulli(0.99)))
        # many follow-up processes 
    end
end
@btime runner2()

```

Indeed, it is likely that this will be faster than the option using the mutable struct. But if you want a mutable struct or not depends on what you want to do with it afterwards.
