# Slower execution with multi-threading using @threads macro

**URL:** <https://discourse.julialang.org/t/slower-execution-with-multi-threading-using-threads-macro/44867>\
**Category:** Performance\
**Tags:** question, parallel, multithreading\
**Created:** [August 13, 2020, 10:17am UTC](https://discourse.julialang.org/t/slower-execution-with-multi-threading-using-threads-macro/44867 "2020-08-13T10:17:18Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![trathi](https://avatars.discourse-cdn.com/v4/letter/t/7c8e57/32.png) [@trathi](https://discourse.julialang.org/u/trathi)\
**Post date:** [August 13, 2020, 10:17am UTC](https://discourse.julialang.org/t/slower-execution-with-multi-threading-using-threads-macro/44867/1 "2020-08-13T10:17:18Z")

</div>

I am running into an issue where allowing multi-threading leads to slower execution time. Following is the simple code where I am testing it:

```julia
function sqr(x)
    return x*x
end

y = zeros(10)
for i=1:8
    y[i] = sqr(i)
end

```

Output:  
`0.008442 seconds (18.07 k allocations: 1.085 MiB)`

Now, allowing multi-threading (8 threads) using `@threads` macro:

```julia
function sqr(x)
    return x*x
end

y = zeros(10)
Threads.@threads for i=1:8
    y[i] = sqr(i)
end

```

Output:  
`0.038529 seconds (20.16 k allocations: 1.068 MiB)`

The code utilizing 8 threads is considerably slower than the one using just 1 thread. This seems to be counter intuitive.

Any suggestions/reasoning on why this could be happening and how to correct it? I am using Julia 1.5.0.

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [August 13, 2020, 10:20am UTC](https://discourse.julialang.org/t/slower-execution-with-multi-threading-using-threads-macro/44867/2 "2020-08-13T10:20:43Z")

</div>

- Read [Performance Tips · The Julia Language](https://docs.julialang.org/en/v1/manual/performance-tips/)
- Specifically, don’t use global scope.
- Use a proper way of benchmarking (using e.g. BenchmarkTools)
- This code is way to small for threading to be useful
- This might not benefit from threading since the operation is so simple that it’s possible that memory speed will bottleneck and not executing instructions.

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [August 13, 2020, 10:27am UTC](https://discourse.julialang.org/t/slower-execution-with-multi-threading-using-threads-macro/44867/3 "2020-08-13T10:27:05Z")

</div>

> [@trathi](#):
>
> This seems to be counter intuitive

Only if you assume that using threads has zero costs. You may be also surprised by this:

```julia
 julia> function test2()
        y = zeros(10)
        Threads.@threads for i=1:8
            nothing
        end
        end

julia> @btime test2()
  1.820 μs (7 allocations: 1.08 KiB)

```

8 Threads which do nothing is slower than your unthreaded code?

---

<div class="post-metadata">

**Author:** ![trathi](https://avatars.discourse-cdn.com/v4/letter/t/7c8e57/32.png) [@trathi](https://discourse.julialang.org/u/trathi)\
**Post date:** [August 13, 2020, 10:39am UTC](https://discourse.julialang.org/t/slower-execution-with-multi-threading-using-threads-macro/44867/4 "2020-08-13T10:39:18Z")

</div>

> [@oheil](#):
>
> 8 Threads which do nothing is slower than your unthreaded code?

Indeed, in this case too the threaded code is slower than the unthreaded code. I am only using this simple case to observe the performance. I have larger code where I observe the same behaviour. Do you have a suggestion on a better way to parallelize independent for loops?

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [August 13, 2020, 11:53am UTC](https://discourse.julialang.org/t/slower-execution-with-multi-threading-using-threads-macro/44867/5 "2020-08-13T11:53:55Z")

</div>

Threads are fine. You should benchmark your real code to see if you get better performance. If not (as you said) there may be something wrong with your code. You can show us your code and perhaps we can try for a sound suggestion.

---

<div class="post-metadata">

**Author:** ![trathi](https://avatars.discourse-cdn.com/v4/letter/t/7c8e57/32.png) [@trathi](https://discourse.julialang.org/u/trathi)\
**Post date:** [August 13, 2020, 12:45pm UTC](https://discourse.julialang.org/t/slower-execution-with-multi-threading-using-threads-macro/44867/6 "2020-08-13T12:45:40Z")

</div>

Thank you @oheil. I will benchmark and see if I could find something that could be causing this issue before posting my large code here. Also, just for reference, I found a discussion on similar issue [here](https://discourse.julialang.org/t/multithreading-now-giving-noticeable-advantage-when-running-parallel-functions/44830).
