parallel is slower than serial

Siarhei Siamashka siarhei.siamashka at gmail.com
Tue Oct 18 16:07:22 UTC 2022


On Tuesday, 18 October 2022 at 11:56:30 UTC, Yura wrote:
> ```D
> // Then for each Sphere, i.e. dot[i]
> // I need to do some arithmetics with itself and other dots
> // I have only parallelized the inner loop, i is fixed.

It's usually a much better idea to parallelize the outer loop. 
Even OpenMP tutorials explain this: 
https://ppc.cs.aalto.fi/ch3/nested/ (check the "collapse it into 
one loop" suggestion from it).

> ```D
> for (auto j=0;j<Ai.length;j++) {
>   A = A ~ Ai[j];
> }
> ```

This way of appending to an array is very slow and `A ~= Ai[j];` 
is much faster. And even better would be `A ~= Ai;` instead of 
the whole loop.


More information about the Digitalmars-d-learn mailing list