Approach 2: Split the Algorithm Into Two Parts
Explore how to optimize C++ algorithms by dividing them into two stages: parallel conditional copying of elements followed by sequential merging to create a contiguous output. Understand performance implications using different predicates and see benchmarking results comparing parallel and standard algorithms.
The second approach is to split the algorithm into two parts. First, the conditional copying is performed in parallel chunks, and then the resulting sparse range is squeezed to a continuous range.
Part one: Copy elements in parallel into the destination range
The first part copies the elements in chunks, resulting in the sparse destination array illustrated below. Each chunk is conditionally copied in parallel, and the resulting range iterators are stored in std::future objects for later retrieval:
The following code implements the first half of the algorithm:
We have now copied the elements (that should ...