# Multithreading for nested loops

**URL:** https://discourse.julialang.org/t/multithreading-for-nested-loops/36002
**Category:** Performance
**Tags:** multithreading
**Created:** [March 15, 2020, 5:31am UTC](https://discourse.julialang.org/t/multithreading-for-nested-loops/36002 "2020-03-15T05:31:15Z")
**Posts on this page:** 3
**Page:** 3

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [January 20, 2022, 1:30pm UTC](https://discourse.julialang.org/t/multithreading-for-nested-loops/36002/41 "2022-01-20T13:30:22Z")

</div>

> [@e3c6](#):
>
> So why do you say it’s problem that too many tasks get spawned here?

Julia can manage huge numbers of tasks — it’s fine (and good for load-balancing) if the number of tasks is far more than the number of processors. But spawning a task has a significant overhead, so you only want to spawn a task for a relatively expensive calculation, not in a tight loop that does a tiny amount of computation per task.

---

<div class="post-metadata">

### Author: ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)
#### Post date: [January 20, 2022, 5:33pm UTC](https://discourse.julialang.org/t/multithreading-for-nested-loops/36002/42 "2022-01-20T17:33:06Z")

</div>

> [@stevengj](#):
>
> But spawning a task has a significant overhead

To have an idea, what’s the overhead (approximately)? Memory, time, both?

---

<div class="post-metadata">

### Author: ![carstenbauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carstenbauer/32/4981_2.png) [@carstenbauer](https://discourse.julialang.org/u/carstenbauer)
#### Post date: [January 20, 2022, 6:00pm UTC](https://discourse.julialang.org/t/multithreading-for-nested-loops/36002/43 "2022-01-20T18:00:37Z")

</div>

> [@e3c6](#):
>
> To have an idea, what’s the overhead (approximately)? Memory, time, both?

Technically both, but the one that typically matters more is time.

To get a feeling for the overhead:

```julia
julia> f() = @sync Threads.@spawn nothing;

julia> @btime f();
  1.753 μs (17 allocations: 960 bytes)

julia> f() = Threads.@spawn nothing;

julia> @btime f();
  377.878 ns (4 allocations: 434 bytes)
```

[Previous page](https://discourse.julialang.org/t/multithreading-for-nested-loops/36002.md?page=2)
