| Start |
|---|
| Introduction to performance C and C++ are well known as high-performance languages - largely due to the heavy amount of code customization, allowing a user to specify performance by choice of structure. When optimizing it is important to benchmark relevant code and completely understand how the code will be used. Common optimization mistakes include:
Typical optimization goals are:
Optimized code can have negative side effects, including:
|
| Empty Base Class Optimization An object cannot occupy less than 1 byte, as then the members of an array of this type would have the same address. Thus In this case, it's not required to allocate a byte for Note that this can only be performed if the first member of |
| Optimizing by executing less code The most straightforward approach to optimizing is by executing less code. This approach usually gives a fixed speed-up without changing the time complexity of the code. Even though this approach gives you a clear speedup, this will only give noticable improvements when the code is called a lot. Removing useless codeFrom C++14, compilers are allowed to optimize this code to remove the allocation and matching deallocation. Doing code only onceA similar approach to this optimization can be used to implement a stable version of Preventing useless reallocating and copying/movingIn the previous example, we already prevented lookups in the std::set, however the |
| Using efficient containers Optimizing by using the right data structures at the right time can change the time-complexity of the code. By using a container which uses a different implementation for storing its elements (hash container instead of tree), we can transform our implementation to complexity N. As a side effect, we will call the comparison operator for std::string less, as it only has to be called when the inserted string should end up in the same bucket. |
| Small Object Optimization Small object optimization is a technique which is used within low level data structures, for instance the By adding extra memory overhead and extra calculations, it tries to prevent an expensive heap allocation. The benefits of this technique are dependent on the usage and can even hurt performance if incorrectly used. ExampleA very naive way of implementing a string with this optimization would the following: As you can see in the code above, some extra complexity has been added in order to prevent some Often it is tried to encode the bool value When to use?As this optimization adds a lot of complexity, it is not recommended to use this optimization on every single class. It will often be encountered in commonly used, low-level data structures. In common C++11 As this optimization only prevents memory allocations when the stored data is smaller than the buffer, it will only give benefits if the class is often used with small data. A final drawback of this optimization is that extra effort is required when moving the buffer, making the move-operation more expensive than when the buffer would not be used. This is especially true when the buffer contains a non-POD type. |
Read Article
https://develop.horje.com/learn/1434/reference