I am using std::find_if on a std::vector. I am interested only in the presence of elements which satisfy the predicate. There are multiple elements in the vector which satisfy the predicate. I have tried without and with execution policy par and par_unseq but it has same result always. Every time it returns the first element satisfying the predicate.
Then why do we have an execution policy with find_if if it has to search in a sequenced way only, and in that case how will par and par_unseq improve the performance?
Disclaimer: I don't know the actual library implementation.
If you use std::find_if, you are telling that you want the first entry that satisfies the predicate. This way, you can find all entries by doing find_if in a loop. If you work in parallel, it can split up the range in parts, so it has less entries to iterate before finding a match, but it has to wait for the first part that has a match in order to give a result and that result should always be the same.
Note that if the match is on the first entry, there is a good chance that the parallel search will be slower, based on the actual implementation, but the worst case with enough entries should be better.
You can get all the matching entries in a range at once with std::copy_if or when using ranges with std::ranges::filter_view.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With