# Why does rand() in threads slowdown speed in Julia 1.3

**URL:** <https://discourse.julialang.org/t/why-does-rand-in-threads-slowdown-speed-in-julia-1-3/32793>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [December 30, 2019, 12:17pm UTC](https://discourse.julialang.org/t/why-does-rand-in-threads-slowdown-speed-in-julia-1-3/32793 "2019-12-30T12:17:06Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sijun](https://avatars.discourse-cdn.com/v4/letter/s/b2d939/32.png) [@Sijun](https://discourse.julialang.org/u/Sijun)\
**Post date:** [December 30, 2019, 12:17pm UTC](https://discourse.julialang.org/t/why-does-rand-in-threads-slowdown-speed-in-julia-1-3/32793/1 "2019-12-30T12:17:06Z")

</div>

In Julia 1.3 each thread has its own default global RNG. Calling rand() in each thread is safe. So the following code that calculates pi returns correct result:

```julia
function par_pi(n::Int)
    hits = zeros(Int, nthreads())
    @threads for i in 1:n
        x, y = rand(), rand()
        hits[threadid()] += (x^2 + y^2 <= 1)
    end

    4.0 * sum(hits) / n
end
@time par_pi(10_000_000)

```

But the above code runs 4x slower than the following version that uses manually allocated RNGs:

```julia
const threadsRNG = [MersenneTwister() for i in 1:nthreads()]

function par_pi2(n::Int)
    hits = zeros(Int, nthreads())
    @threads for i in 1:n
        rng = threadsRNG[threadid()]
        x, y = rand(rng), rand(rng)
        hits[threadid()] += (x^2 + y^2 <= 1)
    end

    4.0 * sum(hits) / n
end
@time par_pi2(10_000_000)

```

How to explain it?

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [December 30, 2019, 12:40pm UTC](https://discourse.julialang.org/t/why-does-rand-in-threads-slowdown-speed-in-julia-1-3/32793/2 "2019-12-30T12:40:49Z")

</div>

`@btime` instead of `@time`?

---

<div class="post-metadata">

**Author:** ![Sijun](https://avatars.discourse-cdn.com/v4/letter/s/b2d939/32.png) [@Sijun](https://discourse.julialang.org/u/Sijun)\
**Post date:** [December 30, 2019, 12:48pm UTC](https://discourse.julialang.org/t/why-does-rand-in-threads-slowdown-speed-in-julia-1-3/32793/3 "2019-12-30T12:48:00Z")

</div>

@jling, thanks for replying.  
I used @btime and the results as follows:

25.503 ms (59 allocations: 7.00 KiB) with new RNGs  
102.228 ms (59 allocations: 7.00 KiB) with default RNGs

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [December 30, 2019, 12:58pm UTC](https://discourse.julialang.org/t/why-does-rand-in-threads-slowdown-speed-in-julia-1-3/32793/4 "2019-12-30T12:58:54Z")

</div>

I can confirm your finding

---

<div class="post-metadata">

**Author:** ![robsmith11](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/robsmith11/32/29641_2.png) [@robsmith11](https://discourse.julialang.org/u/robsmith11)\
**Post date:** [December 30, 2019, 1:01pm UTC](https://discourse.julialang.org/t/why-does-rand-in-threads-slowdown-speed-in-julia-1-3/32793/5 "2019-12-30T13:01:20Z")

</div>

I see the similar results with the latest nightly. It’s strange because `@code_lowered` is basically the same for both functions. Maybe worth filing a bug report.

```julia
julia> @btime par_pi(10^7)
  81.293 ms (44 allocations: 5.89 KiB)
3.140466

julia> @btime par_pi2(10^7)
  27.520 ms (44 allocations: 5.89 KiB)
3.14193

julia> versioninfo()
Julia Version 1.4.0-DEV.670
Commit 032dbe33e3 (2019-12-29 22:39 UTC)
Platform Info:
  OS: Linux (x86_64-pc-linux-gnu)
  CPU: Intel(R) Core(TM) i7-7700 CPU @ 3.60GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-8.0.1 (ORCJIT, skylake)
Environment:
  JULIA_NUM_THREADS = 8

```

EDIT:  
The difference in the lowered code is the following line. I’m not sure how to read it. Do the numbers indicate a different version of the function?  
`Main.:(var"#19#threadsfor_fun#4")`  
`Main.:(var"#2#threadsfor_fun#3")`

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [December 30, 2019, 1:11pm UTC](https://discourse.julialang.org/t/why-does-rand-in-threads-slowdown-speed-in-julia-1-3/32793/7 "2019-12-30T13:11:48Z")

</div>

it’s just the location of function in your REPL

---

<div class="post-metadata">

**Author:** ![Sijun](https://avatars.discourse-cdn.com/v4/letter/s/b2d939/32.png) [@Sijun](https://discourse.julialang.org/u/Sijun)\
**Post date:** [December 30, 2019, 1:33pm UTC](https://discourse.julialang.org/t/why-does-rand-in-threads-slowdown-speed-in-julia-1-3/32793/8 "2019-12-30T13:33:05Z")

</div>

@robsmith11, thank you for confirming the result. Indeed it is strange; Two lowered code are same except for the generated variable names. I will file a bug report.

---

<div class="post-metadata">

**Author:** ![Sijun](https://avatars.discourse-cdn.com/v4/letter/s/b2d939/32.png) [@Sijun](https://discourse.julialang.org/u/Sijun)\
**Post date:** [December 30, 2019, 1:49pm UTC](https://discourse.julialang.org/t/why-does-rand-in-threads-slowdown-speed-in-julia-1-3/32793/9 "2019-12-30T13:49:47Z")

</div>

I’ve filed a bug report:

[https://github.com/JuliaLang/julia/issues/34216](https://github.com/JuliaLang/julia/issues/34216)
