What is Microbenchmark?
A benchmark timing a tiny isolated operation; beware pitfalls such as the compiler optimizing the computation away.
Japanese name: マイクロベンチマーク
A benchmark timing a tiny isolated operation; beware pitfalls such as the compiler optimizing the computation away.
Japanese name: マイクロベンチマーク
A microbenchmark isolates a small operation, such as one function or a short loop, and measures its speed. It is easier to set up than a benchmark that runs a whole application, and suits comparisons that narrow down a cause. It is used, for example, to see which of two ways of writing the same code is faster. One execution is very short, so it is repeated many times and the total time is divided by the count. If 1 million repetitions take 20 ms, each takes 20 ns.
A compiler with optimization enabled may remove a computation whose result is not used, treating it as unnecessary. If the inputs are constants, it may also compute the answer at compile time and embed it. A time of nearly 0 is then reported although the operation under test did not run. The remedy is to supply inputs at run time and actually use the result, for example by printing or summing it. Some benchmarking libraries provide a mechanism that keeps a computation from being removed. Inspecting the generated machine code is also effective.
When a small operation is repeated, its data tends to stay in the cache and branch prediction tends to become more accurate. In a real program other work also uses the cache, so the same operation can be slower. In languages compiled at run time, the first executions are slow and the code speeds up after warm-up. Measurements are repeated several times and the variation is checked. The final effect is confirmed with the time of the whole application.
In many cases the compiler has removed the computation. A computation whose result is unused, or whose result is determined by constants, can disappear under optimization. Make the program actually use the result and measure again.
Not necessarily. If the operation accounts for a small share of the total time, the effect is small too. The cache state also differs from real use, so confirm with a measurement of the whole program.
Microbenchmark appears in 1 practice question across 1 topic, including Performance Analysis.
As an Amazon Associate I earn from qualifying purchases.
This page was drafted with AI and fact-checked by a separate AI review before publication. Our Editorial policy explains how we write and check content. If you spot an error, please tell us on our Contact page.