# @threads vs @parallel, a simple fail case for @threads

**URL:** <https://discourse.julialang.org/t/threads-vs-parallel-a-simple-fail-case-for-threads/6801>\
**Category:** Performance\
**Created:** [October 31, 2017, 1:32pm UTC](https://discourse.julialang.org/t/threads-vs-parallel-a-simple-fail-case-for-threads/6801 "2017-10-31T13:32:48Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![fkjogu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fkjogu/32/2416_2.png) [@fkjogu](https://discourse.julialang.org/u/fkjogu)\
**Post date:** [October 31, 2017, 1:32pm UTC](https://discourse.julialang.org/t/threads-vs-parallel-a-simple-fail-case-for-threads/6801/1 "2017-10-31T13:32:48Z")

</div>

I’m new to Julia. I was experimenting with simple parallel code using threads and processes. Why is

```julia
a = SharedArray{Int64,1}(4)
@time @sync @parallel for i = 1:60000000
    a[myid() - 1] = i
end

# 1.663024 seconds (70.65 k allocations: 3.814 MiB)

```

so much faster and efficient than

```julia
a = SharedArray{Int64,1}(4) # also using a SharedArray for fairness
@time Threads.@threads for i = 1:60000000
    a[Threads.threadid()] = i
end

# 7.977596 seconds (142.59 M allocations: 2.483 GiB, 2.39% gc time)

```

? It seems like the latter allocates a lot. Where does this come from?

I’ve started the REPL with `env JULIA_NUM_THREADS=4 julia -p 4`.

---

<div class="post-metadata">

**Author:** ![aaowens](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aaowens/32/12101_2.png) [@aaowens](https://discourse.julialang.org/u/aaowens)\
**Post date:** [October 31, 2017, 1:50pm UTC](https://discourse.julialang.org/t/threads-vs-parallel-a-simple-fail-case-for-threads/6801/2 "2017-10-31T13:50:38Z")

</div>

First, you need to wrap your code in a function before benchmarking is meaningful. See [https://docs.julialang.org/en/stable/manual/performance-tips.html](https://docs.julialang.org/en/stable/manual/performance-tips.html) .

I think maybe the `@time` macro is interfering with `@threads` in some way? I initially ran the following copied from your code.

```julia
function test2()
       a = SharedArray{Int64,1}(4) # also using a SharedArray for fairness
       @time Threads.@threads for i = 1:60000000
           a[Threads.threadid()] = i
       end
 end

```

This is really slow, and `@code_warntype` complains about a `Core.Box` variable. However, taking the `@time` out of the function fixes this. The following works:

```julia
function test1()
       a = SharedArray{Int64,1}(4)
       @sync @parallel for i = 1:60000000
           a[myid() - 1] = i
       end
 end

function test2()
       a = SharedArray{Int64,1}(4) # also using a SharedArray for fairness
       Threads.@threads for i = 1:60000000
           a[Threads.threadid()] = i
       end
       end

test1() # Warmup 
test2()

```

The output is

```julia
julia> @time test1()
  0.018457 seconds (1.25 k allocations: 48.188 KiB)
4-element Array{Future,1}:
 Future(2, 1, 111, #NULL)
 Future(3, 1, 112, #NULL)
 Future(4, 1, 113, #NULL)
 Future(5, 1, 114, #NULL)

julia> @time test2()
  0.011668 seconds (501 allocations: 18.484 KiB)

```

---

<div class="post-metadata">

**Author:** ![fkjogu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fkjogu/32/2416_2.png) [@fkjogu](https://discourse.julialang.org/u/fkjogu)\
**Post date:** [October 31, 2017, 4:31pm UTC](https://discourse.julialang.org/t/threads-vs-parallel-a-simple-fail-case-for-threads/6801/3 "2017-10-31T16:31:52Z")

</div>

Thank you! That might give a lead, why the allocations.

As it is often the case with such things, I realized my mistake with not wrapping it into a function after posting. But the post was pending for moderation so I couldn’t change it. 🤷‍♂️

But what I wonder, is there a way to precompile the function without running it? Do I need to wrap it into a module and add ` __precompile__ ()` at the top?

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [October 31, 2017, 6:20pm UTC](https://discourse.julialang.org/t/threads-vs-parallel-a-simple-fail-case-for-threads/6801/4 "2017-10-31T18:20:27Z")

</div>

Rather than trying to precompile by hand, you can just use [GitHub - JuliaCI/BenchmarkTools.jl: A benchmarking framework for the Julia language](https://github.com/JuliaCI/BenchmarkTools.jl) to run your function repeatedly and provide a robust estimate of its _actual_ runtime, ignoring compilation.
