I have a simple question. I have defined a struct, and I need to inititate a lot (in the order of millions) of them and loop over them.
I am initiating one at a time and going through the loop as follows:
using Distributions
mutable struct help_me{Z<:Bool}
can_you_help_me::Z
millions_of_thanks::Z
end
for i in 1:max_iter
tmp_help = help_me(rand(Bernoulli(0.5),1)[1],rand(Bernoulli(0.99),1)[1])
# many follow-up processes
end
The memory allocation scales up in max_iter. For my purpose, I do not need to save each struct. Is there a way to "re-use" the memory allocation used by the struct?
Z<:Bool?Boolis already a concrete type? Question 2: any reasonhelp_meneeds to bemutable? immutable structs are often cheaper. also passing arngexplicitly intorandcan give you an easy 2x speedup. (due to multithreading safety reasons)mutablewould fix this. immutable objects can be stack allocated (in most cases), so creating and destroying them should end up being basically free.