# Memory allocations and performance with multithreading

**URL:** <https://discourse.julialang.org/t/memory-allocations-and-performance-with-multithreading/60831>\
**Category:** Performance\
**Tags:** multithreading\
**Created:** [May 9, 2021, 5:47pm UTC](https://discourse.julialang.org/t/memory-allocations-and-performance-with-multithreading/60831 "2021-05-09T17:47:56Z")\
**Posts on this page:** 1\
**Showing post:** 6

<div class="post-metadata">

**Author:** ![Skoffer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skoffer/32/378_2.png) [@Skoffer](https://discourse.julialang.org/u/Skoffer)\
**Post date:** [May 10, 2021, 6:05am UTC](https://discourse.julialang.org/t/memory-allocations-and-performance-with-multithreading/60831/6 "2021-05-10T06:05:57Z")

</div>

> `x_vals` for `n` depends on `n-1` . So, there is no trivial way to parallelize this at the outer loop.

In this particular example it is possible to change order of inner and outer loop, since there is no interaction between `x_vals[i]` and `x_vals[j]`

```julia
function compute_threaded(x0_vals, Δt, nΔt)
        x_vals = copy(x0_vals);
        
        nx = length(x0_vals);
        
        Threads.@threads for j in 1:nx
            for n in 1:nΔt
                x_vals[j] = update(x_vals[j], Δt)
            end
        end
        return x_vals
end

```

but this is probably not what the author wants, since we can assume that in more complicated MWE dependence between different elements exists. In this case, one can use something like [Nbabel nbody integrator speed up - #30 by Skoffer](https://discourse.julialang.org/t/nbabel-nbody-integrator-speed-up/51712/30) but as it was said a few times already, such approaches are useful only for rather large size of vector.

---

_[View the full topic](https://discourse.julialang.org/t/memory-allocations-and-performance-with-multithreading/60831)._
