Microbenchmarking
Profiling can help us find the bottlenecks in our code. If these bottlenecks are caused by inefficient data structures (see Chapter 4, Data Structures), the wrong choice of algorithm (see Chapter 5, Algorithms), or unnecessary contention (see Chapter 11, Concurrency), these bigger issues should be addressed first. But sometimes we find a small function or a small block of code that we need to optimize, and in those cases, we can use a method called microbenchmarking. With this process we create a microbenchmark—a program that runs a small piece of code in isolation from the rest of the program. The process of microbenchmarking consists of the following steps:
- Find a hot spot that needs tuning, preferably using a profiler.
- Separate it from the rest of the code and create an isolated microbenchmark.
- Optimize the microbenchmark. Use a benchmarking framework to test and evaluate the code during optimization.
- Integrate the newly optimized...