r/rakulang • u/[deleted] • Sep 09 '22
Why is Parallel Processing taking more time than Single Processing in Raku?
I stole this code from https://marketing.raku.org/id/1524053519/any ,
and then i timed it to see if the claim was true.
Here is the code that i wrote!
my $time1 = DateTime.now;
(^β).grep(*.is-prime)[100_000].say;
my $time2 = DateTime.now;
say "Execution time in single Processing is {$time2-$time1}";
my $time3 = DateTime.now;
(^β).hyper.grep(*.is-prime)[100_000].say;
my $time4 = DateTime.now;
say "Execution time in parallel Processing is {$time4-$time3}";And then observed that serial processing was much faster than parallel processing...
Here is what my Terminal Printed::
1299721
Execution time in single Processing is 1.09377485
1299721
Execution time in parallel Processing is 1.95315763
3
2
u/gfldex Sep 09 '22
In your case, the optimal batch size actually depends on the size of the primes you check. Right now there is no mechanism to alter batch size after the HyperSeq is formed.
3
2
u/liztormato Rakoon πΊπ¦ ππ» Sep 09 '22
Indeed. This is on a todo list of many people, also on mine. The current design actually makes this pretty hard / hacky to do. So that part of the internals will need a re-design. Maybe someone will do a better version of
hyper/racein module space.
7
u/liztormato Rakoon πΊπ¦ ππ» Sep 09 '22 edited Sep 10 '22
The current algorithm for
.hyperis rather rigid. It always takes a fixed number of entries from the source iterator, and then passes that on to a worker thread. All of that fetching and scheduling means overhead.The default batch-size, as it is called, is 64. This is a way too low number for doing primality checks, as the amount of CPU burnt for checking 64 values is way less than the amount of CPU needed to fetch and schedule. That's why you see the results that you do.
You can change the batch size with the
batchnamed argument. These are some timings from my M1 MacMini:As you can see, using hyper with a much larger batch size, makes it more than 4x as fast.