With the addition of Ranges and Concepts in C++20, our good old algorithm interfaces got super long “rangified” versions. For example, copy is now 4 lines long… and it’s just the declaration!
template <ranges::input_range R, std::weakly_incrementable O> requires std::indirectly_copyable<ranges::iterator_t<R>, O> constexpr ranges::copy_result<ranges::borrowed_iterator_t<R>, O> copy(R&& r, O result); How to decipher such a long declaration?
C++17 brings us parallel algorithms. However, there are not many implementations where you can use the new features. The situation is getting better and better, as we have the MSVC implementation and now Intel’s version will soon be available as the base for libstdc++ for GCC. Since the library is important, I’ve decided to see how to use it and what it offers.
Thank you for all the comments about the string_view performance! Last week I got a lot of feedback on how to improve the initial string split code.
Have a look at how can we update the code and get some better performance.
Intro Last week I showed a few examples of string_view.
Last week you might have read about a few examples of parallel algorithms. Today I have one more application that combines the ideas from the previous post.
We’ll use parallel algorithms and the standard filesystem to count words in all text files in a given directory.
The Case In my previous post, there were two examples: one with iterating over a directory and counting the files sizes and the next one about counting words in a string.
MSVC (VS 2017 15.7, end of June 2018) is as far as I know the only major compiler/STL implementation that has parallel algorithms. Not everything is done, but you can use a lot of algorithms and apply std::execution::par on them!
Have a look at few examples I managed to run.
Writing multithreaded code is hard. You want to utilize all of the machine’s processing power, keep code simple and avoid data races at the same time.
Let’s see how C++17 can make writing parallel code a bit easier.
Intro With C++11⁄14 we’ve finally got threading into the standard library. You can now create std::thread and not just depend on third party libraries or a system API.
Some time ago I’ve seen an inspiring talk from CppCon 2013: “C++ Seasoning” by Sean Parent. One of the main points of this presentation was not to use raw loops. Instead, prefer to use existing algorithms or write functions that ‘wraps’ such loops. I was curious about this idea and searched for nice code examples.