- Hands-On High Performance Programming with Qt 5
- Marek Krajewski
- 188字
- 2021-07-02 13:53:52
Benchmarks
Another tool we can use is benchmarking; this is a technique for measuring the performance of certain isolated features or some synthetic workloads. Benchmarking is best known as a technique for comparing programming languages' or SQL databases' performance, but we can also use it in Qt programming to measure the performance of some aspects of our code.
The Qt Test module contains a QBENCHMARK macro to support such measurements, which is used to denote these lines in a test case that has to be benchmarked, for example:
void BenchmarkTest::test1 {
QBENCHMARK {
// code to be measured ...
}
}
The code inside the macro will be rerun as often, as it is needed to get a stable and accurate measurement. There is a whole lot of logic needed to ensure that benchmark measurements are not biased, but this all is hidden from us by the Qt Test module. However, if we want, the -iterations option can set a fixed number of iterations for the benchmark test. There are more benchmarking macros and options, so please consult the Qt documentation if needed.