# Parallel code seems slow

**URL:** <https://discourse.julialang.org/t/parallel-code-seems-slow/6588>\
**Category:** Performance\
**Created:** [October 20, 2017, 7:38pm UTC](https://discourse.julialang.org/t/parallel-code-seems-slow/6588 "2017-10-20T19:38:33Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![maxtremblay](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/maxtremblay/32/1369_2.png) [@maxtremblay](https://discourse.julialang.org/u/maxtremblay)\
**Post date:** [October 20, 2017, 7:38pm UTC](https://discourse.julialang.org/t/parallel-code-seems-slow/6588/1 "2017-10-20T19:38:33Z")

</div>

Hello,

I have a code that I want to run multiple times with different random inputs to compute an average overall all iterations.

To get more familiar with parallel programming in Julia, I tried to play around with a small example :

```julia
n = 10^3

A = SharedArray{Int}(n)

@time begin
    @sync @parallel for i in 1:n
        A[i] = i
    end
end

```

I ran it a few times and I got some disappointing results.

For n = 10^3, I got

 ![39](https://global.discourse-cdn.com/julialang/original/3X/0/b/0ba848863510947e83e4f927f466892ce0457a37.png)

For n = 10^5, I got

 ![25](https://global.discourse-cdn.com/julialang/original/3X/7/f/7f0055c98f2ec9196739293f1a56c353fa3b3c89.png)

For n = 10^7, I got

 ![16](https://global.discourse-cdn.com/julialang/original/3X/6/1/6104d8ac068706a6a5c03c33e8200396a77cf029.png)

It seems a bit faster to use `julia -p 2` than `julia -p 1`, but it is definitely faster to use only `julia`.

I also try without the parallel stuff and obtain faster times. More precisely, for n = 10^3, 10^5, 10^7, I get

 ![47](https://global.discourse-cdn.com/julialang/original/3X/6/7/67b31e0b6f779bbf71293069e4b6ecf82720dfc3.png)

Finally, I also ran the code on a cluster and got similar results.

Am I doing something wrong or is there a problem with parallel programming in Julia?

---

<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 20, 2017, 8:23pm UTC](https://discourse.julialang.org/t/parallel-code-seems-slow/6588/2 "2017-10-20T20:23:18Z")

</div>

If you’re only running the code once, then you’re probably mostly just timing compilation. At the very least, I would suggest putting the code in a function and then using [GitHub - JuliaCI/BenchmarkTools.jl: A benchmarking framework for the Julia language](https://github.com/JuliaCI/BenchmarkTools.jl) to get a reliable time estimate.

Also, I’m far from an expert in parallel computing, but in an example like the one you’ve posted, in which every operation is trivially cheap, I would suspect that the overhead of sending data between processes vastly outweighs the performance gain from computing `i = i` at each iteration.

---

<div class="post-metadata">

**Author:** ![raminammour](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raminammour/32/13572_2.png) [@raminammour](https://discourse.julialang.org/u/raminammour)\
**Post date:** [October 20, 2017, 8:23pm UTC](https://discourse.julialang.org/t/parallel-code-seems-slow/6588/3 "2017-10-20T20:23:20Z")

</div>

You will need to have your code inside a function and compile it first:

```julia
n = 10^7

A = SharedArray{Int}(n)

function myfill!(A,n)
    @sync @parallel for i in 1:n
        A[i] = i
    end
end
#compile
myfill!(A,20)
#run 
@time myfill!(A,n)
println(A[end])

```

Try timing with the code above.

---

<div class="post-metadata">

**Author:** ![maxtremblay](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/maxtremblay/32/1369_2.png) [@maxtremblay](https://discourse.julialang.org/u/maxtremblay)\
**Post date:** [October 20, 2017, 8:35pm UTC](https://discourse.julialang.org/t/parallel-code-seems-slow/6588/4 "2017-10-20T20:35:09Z")

</div>

Yeah, that was exactly the problem. I taught that the @parallel and @sync would be pre-compile. I was wrong.

Thanks !
