This is the code repository for C++17 High Performance, published by Packt. It contains all the supporting project files necessary to work through the book from start to finish.
C++ is an easy language and can be used to write complex applications and performance-critical code. It has evolved and changed over the last few years. This book will guide you through optimizing the performance of your C++ apps by allowing them to consume fewer resources on the device they’re running on.
The book begins with a gentle introduction to help you identify the bottlenecks in C++. We move on to measuring performance and you’ll see how this affects the way you write code. Next you’ll understand the importance of data structure optimization and how it can be used efficiently. After that, we’ll explain which algorithm should be used to achieve faster execution, followed by how to use STL containers.
Moving on, you’ll find out how to boost your code’s performance by executing compiled code. You’ll get hands-on making use of multiple cores to enable efficient and faster execution, including GPUs. After that, you’ll learn how to improve memory management in C++. We end with some techniques you can use to optimize the performance of your apps.
All of the code is organized into folders. Each folder starts with a number followed by the application name. For example, Chapter02.
The code will look like the following:
#include <iostream>
auto main() -> int {
std::cout << "High Performance C++\n";
}
To get the most out of this book, you need to have a basic knowledge of C++. It's preferable if you have already been facing problems related to performance and are now looking for new tools and practices to have ready the next time you need to work with performance and C++.
There are a lot of code examples in this book. Some are taken from the real world, but most of them are artificial or vastly simplified examples to prove a concept rather than providing you with production-ready code. We have put all the code examples in source files divided by chapter so that it is fairly easy to find the examples you want to experiment with. If you open up the source code files, you will note that we have replaced most of the main() functions from the examples with test cases written with Google Test framework. We hope that this will help you rather than confuse you. It allowed us to write helpful descriptions for each example, and it also makes it easier to run all the examples from one chapter at once.
In order to compile and run the examples, you will need the following:
A computer An operation system (we have verified the examples on Windows and macOS) A compiler (we have been using Clang, GCC, and Microsoft Visual C++) CMake The CMake script provided with the example code will download and install further dependencies such as Boost, OpenCL, and Google Test.
During the writing of this book, it has been of great help for us to use Compiler Explorer, which is available at https://godbolt.org/. Compiler Explorer is an online compiler service that lets you try various compilers and versions. Try it out if you haven't already!
Click here if you have any feedback or suggestions.