# Huge performance drop using @spawn

**URL:** <https://discourse.julialang.org/t/huge-performance-drop-using-spawn/33242>\
**Category:** General Usage\
**Tags:** performance\
**Created:** [January 11, 2020, 5:05pm UTC](https://discourse.julialang.org/t/huge-performance-drop-using-spawn/33242 "2020-01-11T17:05:35Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![wizebt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wizebt/32/12256_2.png) [@wizebt](https://discourse.julialang.org/u/wizebt)\
**Post date:** [January 11, 2020, 5:05pm UTC](https://discourse.julialang.org/t/huge-performance-drop-using-spawn/33242/1 "2020-01-11T17:05:36Z")

</div>

Using Jeff’s article [https://julialang.org/blog/2019/07/multithreading](https://julialang.org/blog/2019/07/multithreading)

using  
julia\> VERSION  
v"1.3.1"  
I simply tried the first example with JULIA\_NUM\_THREADS=4 on a 12 core machine with 64GB RAM

```julia
import Base.Threads.@spawn

function fib(n::Int)
    if n < 2
        return n
    end
    t = @spawn fib(n - 2)
    return fib(n - 1) + fetch(t)
end

```

julia\> @btime fib(20)  
4.473 ms (93843 allocations: 8.11 MiB)  
6765

While running the same without @spawn we get  
julia\> @btime fib(20)  
26.614 μs (0 allocations: 0 bytes)  
6765

Conclusion using @spawn just brings huge counter perf and heavy memory usage.

Can anyone clarify for me please why  
Thanks

---

<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:** [January 11, 2020, 5:11pm UTC](https://discourse.julialang.org/t/huge-performance-drop-using-spawn/33242/2 "2020-01-11T17:11:03Z")

</div>

The problem is that almost all of the calls fib makes are to fib(1) and fib(2). Since spawn has about a 1 msec time, you would only want to use it when the inside computation takes more than that amount of time.

---

<div class="post-metadata">

**Author:** ![wizebt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wizebt/32/12256_2.png) [@wizebt](https://discourse.julialang.org/u/wizebt)\
**Post date:** [January 11, 2020, 5:15pm UTC](https://discourse.julialang.org/t/huge-performance-drop-using-spawn/33242/3 "2020-01-11T17:15:45Z")

</div>

I get the Point Oscar, thks for you immediate reply 😉

---

<div class="post-metadata">

**Author:** ![cshen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cshen/32/217287_2.png) [@cshen](https://discourse.julialang.org/u/cshen)\
**Post date:** [January 11, 2020, 5:27pm UTC](https://discourse.julialang.org/t/huge-performance-drop-using-spawn/33242/4 "2020-01-11T17:27:13Z")

</div>

See also [this](https://discourse.julialang.org/t/simple-recursive-fibonaci-example-how-to-make-it-faster/32369/16) as it was already discussed in detail!

tl;dr start using spawn only with big enough ns.
