Microbenchmarking
Learn about microbenchmarking for optimization and Amdahl's law to speedup.
We'll cover the following
Optimizing small code blocks
Profiling can help us find the bottlenecks in our code. If these bottlenecks are caused by inefficient data structures, the wrong choice of algorithm, or unnecessary contention, 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 code into the program and measure again to see if the optimizations are relevant when the code runs in a bigger context with more relevant input.
The four steps of the process are illustrated in the following figure:
Get hands-on with 1200+ tech skills courses.