# Misunderstanding Allocs for Monte Carlo summation

**URL:** <https://discourse.julialang.org/t/misunderstanding-allocs-for-monte-carlo-summation/115894>\
**Category:** Performance\
**Tags:** memory-allocation\
**Created:** [June 19, 2024, 11:56pm UTC](https://discourse.julialang.org/t/misunderstanding-allocs-for-monte-carlo-summation/115894 "2024-06-19T23:56:29Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Spuriosity1](https://avatars.discourse-cdn.com/v4/letter/s/eb9ed0/32.png) [@Spuriosity1](https://discourse.julialang.org/u/Spuriosity1)\
**Post date:** [June 19, 2024, 11:56pm UTC](https://discourse.julialang.org/t/misunderstanding-allocs-for-monte-carlo-summation/115894/1 "2024-06-19T23:56:29Z")

</div>

I have a simple test function here:

```julia
function tester(nreps::Int, n_x::Int)
    res = zeros(Float64, n_x)
    for _ = 1:nreps
        res += rand(Float64, n_x)
    end
    return res
end

```

I do not understand the behaviour /meaning of Allocs as reported by BenchmarkTools  
`@benchmark tester(10, 1000) -> allocs estimate 21`  
`@benchmark tester(100, 1000) -> allocs estimate 201`

Replacing `rand` by `rand!` helps a little bit:

```julia
function tester(nreps::Int, n_x::Int)
    res = zeros(Float64, n_x)
    tmp = zeros(Float64, n_x)
    for _ = 1:nreps
        rand!(tmp)
        res += tmp
    end
    return res
end

```

but again I find that increasing `nreps` by a factor of 10 also increases allocations by a factor of 10.

Equivalent code in e.g. C should require the same memory for all values of nreps, since I am simply mutating an array in-place. What is Julia doing here, and how do I avoid the behaviour?

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [June 20, 2024, 12:00am UTC](https://discourse.julialang.org/t/misunderstanding-allocs-for-monte-carlo-summation/115894/2 "2024-06-20T00:00:44Z")

</div>

`rand(Float64, n_x)` is allocating an array of `n_x` elements. You probably want `res .+= rand.(Float64)`

---

<div class="post-metadata">

**Author:** ![Spuriosity1](https://avatars.discourse-cdn.com/v4/letter/s/eb9ed0/32.png) [@Spuriosity1](https://discourse.julialang.org/u/Spuriosity1)\
**Post date:** [June 20, 2024, 12:11am UTC](https://discourse.julialang.org/t/misunderstanding-allocs-for-monte-carlo-summation/115894/3 "2024-06-20T00:11:49Z")

</div>

So in general, the fast way to write this (with the understanding that rand! will be replaced by some other nontrivial function) is

```julia
function tester(nreps::Int, n_x::Int)
    res = zeros(Float64, n_x)
    tmp = zeros(Float64, n_x)
    for _ = 1:nreps
        rand!(tmp)
        res .+= tmp
    end
    return res
end

```

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [June 20, 2024, 12:55am UTC](https://discourse.julialang.org/t/misunderstanding-allocs-for-monte-carlo-summation/115894/4 "2024-06-20T00:55:14Z")

</div>

exactly.
