jeudi 29 décembre 2022

Mutate existing container for `rand` results in Julia?

I have a simulation where the computation will reuse the random sample of a multi-dimensional random variable. I'd like to be able to re-use a pre-allocated container for better performance with less allocations.

Simplified example:

function f()
    container = zeros(2) # my actual use-case is an MvNormal
    map(1:100) do i
        container = rand(2)
        sum(container)
    end
end

I think the above is allocating a new vector as a result of rand(2) each time. I'd like to mutate container by storing the results of the rand call.

I tried the typical pattern of rand!(container,...) but there does not seem to be a built-in rand! function following the usual mutation convention in Julia.

How can I reuse the container or otherwise improve the performance of this approach?




Aucun commentaire:

Enregistrer un commentaire