They say “performance is king’… It was true a decade ago and it certainly is now. With more and more data the world generates each day, we need more and more computing power to process it.
It used to be the case that some SW vendors preferred to wait for a new generation of HW to speed up their application and did not spend human resources on making improvements in their code.
Last week’s article about smaller C++17 features mentioned updated operator new() that handles non-standard alignment of objects. How does it work? Can you use it to ask for arbitrary alignments? Let’s try some code and have a closer look.
Last update: 9th September 2019
Why should you care about alignment?
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.
Searchers from C++17 are a new way to perform efficient pattern lookups. The new standard offers three searchers: default_searcher , boyer_moore_searcher and boyer_moore_horspool_searcher. The last two implements algorithms that require some additional preprocessing for the input pattern. Is there a chance to separate preprocessing time from the search time?
With C++17 you can now use more sophisticated algorithms for pattern searches! Now, you’ll have more control and a promising performance boost for many use cases.
Intro The naive approach of finding a pattern in a string is O(nm) (where n is the length of the whole string, m is the length of the pattern).
How do you initialise a string member in the constructor? By using const string&, string value and move, string_view or maybe something else?
Let’s have a look at possible options.
Intro Below there’s a simple class with one string member. We’d like to initialise it.
For example:
class UserName { std::string mName; public: UserName(const std::string& str) : mName(str) { } }; As you can see a constructor is taking const std::string& str.
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.
How much is std::string_view faster than standard std::string operations?
Have a look at a few examples where I compare std::string_view against std::string.
Intro I was looking for some examples of string_view, and after a while, I got curious about the performance gain we might get.
string_view is conceptually only a view of the string: usually implemented as[ptr, length].
As you might notice from reading this blog, I love doing performance optimizations. Let’s take some algorithm or some part of the app, understand it and then improve, so it works 5x… or 100x faster! Doesn’t that sound awesome?
I hope that you answered “Yes” to the question in the introduction.
When doing my last performance tests for bool packing, I got strange results sometimes. It appeared that one constant generated different results than the other. Why was that? Let’s have a quick look at branching performance.
The problem Just to recall (first part, second part) I wanted to pack eight booleans (results of a condition) into one byte, 1 bit per condition result.
Let’s continue with the topic of packing boolean arrays into bits. Last time I’ve shown a basic - single threaded version of this ‘super’ advanced algorithm. By using more independent variables, we could speed things up and go even faster than no packing version! We’ve also used std::vector and std::bitset.
Imagine you have an array of booleans (or an array of ‘conditions’), and you want to pack it - so you use only one bit per boolean. How to do it? Let’s do some experiments!
Updated: 8th May 2017
Read the second part here and also one update.
Motivation I started writing this post because I came across a similar problem during my work some time ago.