# @threads and vmap

**URL:** <https://discourse.julialang.org/t/threads-and-vmap/127340>\
**Category:** Performance\
**Created:** [March 25, 2025, 11:08am UTC](https://discourse.julialang.org/t/threads-and-vmap/127340 "2025-03-25T11:08:13Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lincoln\_Hannah](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lincoln_hannah/32/19198_2.png) [@Lincoln\_Hannah](https://discourse.julialang.org/u/Lincoln_Hannah)\
**Post date:** [March 25, 2025, 11:08am UTC](https://discourse.julialang.org/t/threads-and-vmap/127340/1 "2025-03-25T11:08:13Z")

</div>

Is it possible to speed this up further?

```julia
f(x) = sin(x)cos(x)sin(x)cos(x)
X = repeat( [randn(10^7)], 5 )

@time for x in X; map(f,x) end # 1.0s
@time Threads.@threads for x in X; map(f,x) end # 0.3s
@time Threads.@threads for x in X; vmap(f,x) end # 0.1s

```

---

<div class="post-metadata">

**Author:** ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)\
**Post date:** [March 25, 2025, 11:35am UTC](https://discourse.julialang.org/t/threads-and-vmap/127340/2 "2025-03-25T11:35:00Z")

</div>

It’d be easier to help if you at the very minimum said what package you got `vmap` from.

Asking for help making code faster when the code isn’t runnable almost never results in high quality help.

---

<div class="post-metadata">

**Author:** ![Lincoln\_Hannah](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lincoln_hannah/32/19198_2.png) [@Lincoln\_Hannah](https://discourse.julialang.org/u/Lincoln_Hannah)\
**Post date:** [March 25, 2025, 11:52am UTC](https://discourse.julialang.org/t/threads-and-vmap/127340/3 "2025-03-25T11:52:20Z")

</div>

Loop Vectorization  
Sorry i forgot there were other vmaps

---

<div class="post-metadata">

**Author:** ![barucden](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/barucden/32/26154_2.png) [@barucden](https://discourse.julialang.org/u/barucden)\
**Post date:** [March 25, 2025, 11:56am UTC](https://discourse.julialang.org/t/threads-and-vmap/127340/4 "2025-03-25T11:56:08Z")

</div>

If this `f` is your actual function, you can simplify it:

```julia
f(x) = (sin(2 * x) / 2)^2

```

This definition is 5x faster on my computer.

Some time is spent allocating the result of `map`. The result always has the same shape, so you could pre-allocate the array and use `map!`.

---

<div class="post-metadata">

**Author:** ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)\
**Post date:** [March 25, 2025, 12:00pm UTC](https://discourse.julialang.org/t/threads-and-vmap/127340/5 "2025-03-25T12:00:44Z")

</div>

An easy target here would be to just do 1/5th the amount of work and do

```julia
map(f, X[1])

```

since

> [@Lincoln\_Hannah](#):
>
> `X = repeat( [randn(10^7)], 5 )`

contains 5 instances of the identically same array.

---

<div class="post-metadata">

**Author:** ![sgaure](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sgaure/32/14779_2.png) [@sgaure](https://discourse.julialang.org/u/sgaure)\
**Post date:** [March 25, 2025, 12:13pm UTC](https://discourse.julialang.org/t/threads-and-vmap/127340/6 "2025-03-25T12:13:46Z")

</div>

> [@Lincoln\_Hannah](#):
>
> `for x in X; map(f,x) end`

It is a bit difficult to grasp what you are asking for. Your example code does not produce any output. So it can be be replaced with `nothing`, which typically executes in 0.0 seconds.

There are good reasons that we need to know a bit more about the problem, because one of the things that matters when using threads is which results should be accumulated. Is it a short computation inside the loop, a long computation? There are some general advice, and there are some methods which works on particular types of code.
