The parallelism is slower than sequential computing?



I am newbie to Julia and I was testing Julia for the computational complexity of small program, as listed on
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.

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.


Using tic() and toc() will measure compile time. One should use 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.

  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.


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.


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 @, 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.


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.


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.