# The parallelism is slower than sequential computing?

**URL:** <https://discourse.julialang.org/t/the-parallelism-is-slower-than-sequential-computing/4014>\
**Category:** General Usage\
**Tags:** question\
**Created:** [May 31, 2017, 1:55pm UTC](https://discourse.julialang.org/t/the-parallelism-is-slower-than-sequential-computing/4014 "2017-05-31T13:55:52Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![nagnapk](https://avatars.discourse-cdn.com/v4/letter/n/ea5d25/32.png) [@nagnapk](https://discourse.julialang.org/u/nagnapk)\
**Post date:** [May 31, 2017, 1:55pm UTC](https://discourse.julialang.org/t/the-parallelism-is-slower-than-sequential-computing/4014/1 "2017-05-31T13:55:52Z")

</div>

Hello,  
I am newbie to Julia and I was testing Julia for the computational complexity of small program, as listed on  
[https://julialang.org/](https://julialang.org/).  
I run the same program in sequential loop and surprisingly, at least to me, the sequential computing output quickly.  
The screen shot of both formats with elapsed time is shown as followings.

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

I have perception that parallel computing must be faster than its sequential counterpart. Is it true?  
If so then how the results in the figure varies?

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![ExpandingMan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/expandingman/32/866_2.png) [@ExpandingMan](https://discourse.julialang.org/u/ExpandingMan)\
**Post date:** [May 31, 2017, 2:27pm UTC](https://discourse.julialang.org/t/the-parallelism-is-slower-than-sequential-computing/4014/2 "2017-05-31T14:27:22Z")

</div>

Using `tic()` and `toc()` will measure compile time. One should use [BenchmarkTools.jl](https://github.com/JuliaCI/BenchmarkTools.jl).

Note also that these two programs are not equivalent. The parallel form performs a summation (since you’ve inserted a `(+)`), while the sequential form does not.

---

<div class="post-metadata">

**Author:** ![anon94023334](https://avatars.discourse-cdn.com/v4/letter/a/e274bd/32.png) [@anon94023334](https://discourse.julialang.org/u/anon94023334)\
**Post date:** [May 31, 2017, 2:28pm UTC](https://discourse.julialang.org/t/the-parallelism-is-slower-than-sequential-computing/4014/3 "2017-05-31T14:28:15Z")

</div>

1. How many processors did you start julia with (`julia -p N`, or `addprocs(n)`).
2. Run at least twice; better yet, use BenchmarkTools.

Also: Parallel processing has setup overhead that serial processing doesn’t.

Also also: what @expandingman said re: equivalence of code.

---

<div class="post-metadata">

**Author:** ![favba](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/favba/32/2735_2.png) [@favba](https://discourse.julialang.org/u/favba)\
**Post date:** [May 31, 2017, 2:29pm UTC](https://discourse.julialang.org/t/the-parallelism-is-slower-than-sequential-computing/4014/4 "2017-05-31T14:29:44Z")

</div>

The codes don’t do the same thing. The parallel code is reducing (summing up) the results of `rand` on `nheads`. Th sequetial code is just generating random numbers and not storing it or summing it…

Also, to time things properly in Julia you should put your code in a function, call it once, so it gets compiled and then time.

I better way to time a function `f` would be to use the `@time` macro. Like this:

`@time f()`

EDIT: A true better way is to use the Benchmark tools as said above.

---

<div class="post-metadata">

**Author:** ![nagnapk](https://avatars.discourse-cdn.com/v4/letter/n/ea5d25/32.png) [@nagnapk](https://discourse.julialang.org/u/nagnapk)\
**Post date:** [May 31, 2017, 8:15pm UTC](https://discourse.julialang.org/t/the-parallelism-is-slower-than-sequential-computing/4014/5 "2017-05-31T20:15:57Z")

</div>

Yes, I mistakenly forget to count the number of heads in writing the sequential version.  
The parallel code for counting the number of heads, the code @ [https://julialang.org/](https://julialang.org/), completes the execution in 2.323782322 seconds on quadcore i5-6300u. The sequential one completes in 2.459187005 seconds. These figures are obtained using tic, toc routine.  
I shall make use of the BenchmarkTools as I learn the tools.  
Anyway, thanks all of you for your kind support.  
asif

---

<div class="post-metadata">

**Author:** ![anon94023334](https://avatars.discourse-cdn.com/v4/letter/a/e274bd/32.png) [@anon94023334](https://discourse.julialang.org/u/anon94023334)\
**Post date:** [June 1, 2017, 1:04am UTC](https://discourse.julialang.org/t/the-parallelism-is-slower-than-sequential-computing/4014/6 "2017-06-01T01:04:52Z")

</div>

But - are you adding processes via `addprocs` when you run the parallel code? If not, all you’re seeing is the overhead of setting up a parallel job to run on a single processor.

---

<div class="post-metadata">

**Author:** ![nagnapk](https://avatars.discourse-cdn.com/v4/letter/n/ea5d25/32.png) [@nagnapk](https://discourse.julialang.org/u/nagnapk)\
**Post date:** [June 1, 2017, 3:00am UTC](https://discourse.julialang.org/t/the-parallelism-is-slower-than-sequential-computing/4014/7 "2017-06-01T03:00:48Z")

</div>

Dear sbromberger I forget you mention about the function in your very first post.  
However, I perceived that writing @parallel shall automatically distribute the processing across all of the available CPU.  
Anyway I shall look at the function to use in the future.  
Thank you sbromberger for your time and valued comments.
