# Runtime increasing as I do more replications

**URL:** https://discourse.julialang.org/t/runtime-increasing-as-i-do-more-replications/65850
**Category:** Performance
**Created:** [August 5, 2021, 12:23am UTC](https://discourse.julialang.org/t/runtime-increasing-as-i-do-more-replications/65850 "2021-08-05T00:23:22Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![worldgiraffe2](https://avatars.discourse-cdn.com/v4/letter/w/a5b964/32.png) [@worldgiraffe2](https://discourse.julialang.org/u/worldgiraffe2)
#### Post date: [August 5, 2021, 12:23am UTC](https://discourse.julialang.org/t/runtime-increasing-as-i-do-more-replications/65850/1 "2021-08-05T00:23:23Z")

</div>

Hi, I’m a new Julia user and would greatly appreciate your help.

Currently, I’m running a simulation with multiple replications, as below

```julia
glob_result = []
for i in 1:n_rep
    Random.seed!(1234+i);
    model_result = @time simulation(param)
    append!(glob_result, [model_result])
end

```

As I append the recent result to glob\_result, the simulation time increases like below

```julia
 1.758594 seconds (3.05 M allocations: 1.112 GiB, 11.81% gc time)
  1.863981 seconds (3.05 M allocations: 1.112 GiB, 10.96% gc time)
... to 
  7.548435 seconds (3.05 M allocations: 1.112 GiB, 63.28% gc time)
  8.421129 seconds (3.05 M allocations: 1.128 GiB, 75.80% gc time)
...
 11.236033 seconds (3.05 M allocations: 1.143 GiB, 80.77% gc time)
 20.993897 seconds (3.05 M allocations: 1.097 GiB, 86.13% gc time)
 44.633841 seconds (3.05 M allocations: 1.112 GiB, 93.60% gc time)
 36.744767 seconds (3.05 M allocations: 1.143 GiB, 92.03% gc time)
 25.545203 seconds (3.05 M allocations: 1.112 GiB, 91.43% gc time)
 43.805692 seconds (3.05 M allocations: 1.112 GiB, 91.81% gc time)
 57.387221 seconds (3.05 M allocations: 1.112 GiB, 94.57% gc time)

```

The increasing runtime does not appear when I don’t append the simulation result to global result.

I saw some posts advising to use struct. My type of model\_result is Vector{Vector{Vector{NamedArray}}}. If I make it into struct type, would it help? Or, could someone help me how I can solve this issue? Thank you very much.

---

<div class="post-metadata">

### Author: ![Tomas\_Pevny](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomas_pevny/32/25466_2.png) [@Tomas\_Pevny](https://discourse.julialang.org/u/Tomas_Pevny)
#### Post date: [August 5, 2021, 4:48am UTC](https://discourse.julialang.org/t/runtime-increasing-as-i-do-more-replications/65850/2 "2021-08-05T04:48:54Z")

</div>

You are spending more and more time in garbage collection. You might not be releasing all the memory.

---

<div class="post-metadata">

### Author: ![mcreel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcreel/32/30088_2.png) [@mcreel](https://discourse.julialang.org/u/mcreel)
#### Post date: [August 5, 2021, 6:26am UTC](https://discourse.julialang.org/t/runtime-increasing-as-i-do-more-replications/65850/3 "2021-08-05T06:26:47Z")

</div>

The vector glob\_result is of type Any, and it is constantly resized by append!. Initialize glob\_result with the correct size and type, and it should be a lot faster.

---

<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: [August 5, 2021, 10:10am UTC](https://discourse.julialang.org/t/runtime-increasing-as-i-do-more-replications/65850/4 "2021-08-05T10:10:11Z")

</div>

Is it possible to show a minimal code that we can run? I suspect that the issue is somewhere else, at least for me it does not make sense thar the running time of the solver is dependent on how one collects the result outside the function.

---

<div class="post-metadata">

### Author: ![worldgiraffe2](https://avatars.discourse-cdn.com/v4/letter/w/a5b964/32.png) [@worldgiraffe2](https://discourse.julialang.org/u/worldgiraffe2)
#### Post date: [August 5, 2021, 8:36pm UTC](https://discourse.julialang.org/t/runtime-increasing-as-i-do-more-replications/65850/5 "2021-08-05T20:36:12Z")

</div>

Hi everyone, thank you very much for you reply! All your replies helped me.  
Through some research, I learned to define type, allocate memory, and improve original simulation code.  
For my code, I changed NamedArrays to Arrays with const. This resolved the runtime increasing issue. I will further optimize my code with specifying type and memory. Thanks!
