# Alternative to Any\[ \] ? or efficient usage idea?

**URL:** <https://discourse.julialang.org/t/alternative-to-any-or-efficient-usage-idea/116969>\
**Category:** New to Julia\
**Tags:** question, memory-allocation, push\
**Created:** [July 12, 2024, 12:33pm UTC](https://discourse.julialang.org/t/alternative-to-any-or-efficient-usage-idea/116969 "2024-07-12T12:33:08Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sahil\_Khan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sahil_khan/32/47573_2.png) [@Sahil\_Khan](https://discourse.julialang.org/u/Sahil_Khan)\
**Post date:** [July 12, 2024, 12:33pm UTC](https://discourse.julialang.org/t/alternative-to-any-or-efficient-usage-idea/116969/1 "2024-07-12T12:33:08Z")

</div>

Hello everyone, Im trying to avoid use of `any[]` in my code but at the moment situation is such that I need to save results for unknown number of interactions (stochastic in nature). So, I’m using `any[]` and pushing results in it for each interaction.

Are there any tips on using `any[]` that i should know to make it memory efficient ?

Thank you

---

<div class="post-metadata">

**Author:** ![DanielVandH](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/danielvandh/32/31134_2.png) [@DanielVandH](https://discourse.julialang.org/u/DanielVandH)\
**Post date:** [July 12, 2024, 12:39pm UTC](https://discourse.julialang.org/t/alternative-to-any-or-efficient-usage-idea/116969/2 "2024-07-12T12:39:00Z")

</div>

Is your intention to just create an initial array for pushing into? If all your results are of the same type, say `T`, you can just do `T[]` and then `push` into it, e.g. something like

```julia
results = T[] # same as Vector{T}()
while condition() # replace with whatever you're using to determine when to stop iteration
    result = simulate()
    push!(results, result)
end

```

If you have a rough estimate for the number of interations, say `n`, you should also do `sizehint!(results, n)` before the loop

---

<div class="post-metadata">

**Author:** ![Sahil\_Khan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sahil_khan/32/47573_2.png) [@Sahil\_Khan](https://discourse.julialang.org/u/Sahil_Khan)\
**Post date:** [July 12, 2024, 12:41pm UTC](https://discourse.julialang.org/t/alternative-to-any-or-efficient-usage-idea/116969/3 "2024-07-12T12:41:41Z")

</div>

At the moment i’m saving different types stored in a tuple. So something like this

```julia
push!(x, (x1, x2, x3, x4, x5)). # which has float, int also symbol

```

---

<div class="post-metadata">

**Author:** ![DanielVandH](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/danielvandh/32/31134_2.png) [@DanielVandH](https://discourse.julialang.org/u/DanielVandH)\
**Post date:** [July 12, 2024, 12:43pm UTC](https://discourse.julialang.org/t/alternative-to-any-or-efficient-usage-idea/116969/4 "2024-07-12T12:43:16Z")

</div>

You could use e.g. `T = Tuple{Float64, Int, Symbol, (whatever else)}`. Something more common would be to define a struct for your results and then push into a vector of that, e.g.

```julia
struct SimulationResult
    x1::Int 
    x2::Symbol 
    x3::Float64 
    < and so on >
end 
results = SimulationResult[]
< the rest >

```

---

<div class="post-metadata">

**Author:** ![Sahil\_Khan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sahil_khan/32/47573_2.png) [@Sahil\_Khan](https://discourse.julialang.org/u/Sahil_Khan)\
**Post date:** [July 12, 2024, 12:44pm UTC](https://discourse.julialang.org/t/alternative-to-any-or-efficient-usage-idea/116969/5 "2024-07-12T12:44:45Z")

</div>

i will try this and come back 🙂

---

<div class="post-metadata">

**Author:** ![screw\_dog](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/screw_dog/32/48119_2.png) [@screw\_dog](https://discourse.julialang.org/u/screw_dog)\
**Post date:** [July 12, 2024, 12:46pm UTC](https://discourse.julialang.org/t/alternative-to-any-or-efficient-usage-idea/116969/6 "2024-07-12T12:46:25Z")

</div>

As a general aside: don’t worry too much about optimising until you’ve got something that works and you can profile it. If your code is spending most of it’s time doing simulations then this may well not be critical.

---

<div class="post-metadata">

**Author:** ![Sahil\_Khan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sahil_khan/32/47573_2.png) [@Sahil\_Khan](https://discourse.julialang.org/u/Sahil_Khan)\
**Post date:** [July 12, 2024, 12:47pm UTC](https://discourse.julialang.org/t/alternative-to-any-or-efficient-usage-idea/116969/7 "2024-07-12T12:47:58Z")

</div>

My code is working fine. Only issue I have right now is too much time consumption and memory. So, just for 5 simulation it consumes almost 5 GiB which seems insane. 😂

---

<div class="post-metadata">

**Author:** ![DanielVandH](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/danielvandh/32/31134_2.png) [@DanielVandH](https://discourse.julialang.org/u/DanielVandH)\
**Post date:** [July 12, 2024, 12:49pm UTC](https://discourse.julialang.org/t/alternative-to-any-or-efficient-usage-idea/116969/8 "2024-07-12T12:49:50Z")

</div>

That would probably indicate that this `Any` might be 0.00001% of your troubles 😅.

My solution does answer what you asked originally, but you may need to look at [Performance Tips · The Julia Language](https://docs.julialang.org/en/v1/manual/performance-tips/) (bit long, so browse the section titles first to see what might be most relevant to you - memory usage like that sounds something like a type instability somewhere / global variables etc.).Make sure to use functions everywhere rather than staying in global scope!

---

<div class="post-metadata">

**Author:** ![mike.ingold](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mike.ingold/32/203749_2.png) [@mike.ingold](https://discourse.julialang.org/u/mike.ingold)\
**Post date:** [July 12, 2024, 5:54pm UTC](https://discourse.julialang.org/t/alternative-to-any-or-efficient-usage-idea/116969/9 "2024-07-12T17:54:48Z")

</div>

It’s hard to comment on possible causes without seeing some code or a better understanding of what it’s doing.

Possibly-relevant tips:

- Creating an empty vector and repeatedly `push!`ing into it isn’t super efficient. Sometimes this can be addressed by generating the vector using `map`, e.g. when you expect every function call to result in an entry. You could also look into whether a `sizehint!` might help this, but that might be a long shot.
- If you’re running any operations where vectors/matrices are updated without using in-place operations, you can create a situation where new memory is allocated but old memory isn’t immediately freed, which can sometimes lead to memory usage multiple times greater than you expected.

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [July 12, 2024, 9:19pm UTC](https://discourse.julialang.org/t/alternative-to-any-or-efficient-usage-idea/116969/10 "2024-07-12T21:19:53Z")

</div>

### Quick tip:

Whenever you have a for loop like

```julia
ys = []
for x in xs
  push!(ys, f(x))
end

```

replace it with the `map-do` syntax:

```julia
ys = map(xs) do x
  f(x)
end

```

It will infer the resulting type even when `f` is a very complicated algorithm.
