typedef fcf::NTest::Duration
Type: fcf::NTest::Duration
Definition:
typedef
fcf::NTest::DurationBasic<fcf::NTest::SteadyClock> Duration;
Package: fcfTest
File: test.hpp
Available from version: 1.1.14
A high-resolution benchmarking utility for measuring execution time of code blocks or loops.
The fcf::NTest::Duration class is a powerful tool designed for micro-benchmarking within unit tests. It allows developers to measure the execution time of specific code segments, whether they are wrapped in a lambda function or executed within a manual loop.
Key features include:
- Automatic Iteration: When provided with a lambda, it can automatically run the code for a specified number of iterations to ensure statistical significance.
- Manual Control: Provides begin and end methods for fine-grained control over measurement boundaries.
- Statistical Data: After execution, it provides access to total duration, average time, minimum time, maximum time, and a detailed histogram of results.
- High Precision: Uses high-resolution steady clocks to capture nanosecond-level measurements.
Example: Benchmarking a Lambda Function
Demonstrating the most common way to use fcf::NTest::Duration: passing a lambda function to be executed for a specific number of iterations.
#define FCF_TEST_IMPLEMENTATION
#include <fcfTest/test.hpp>
#include <vector>
#include <algorithm>
FCF_TEST_DEFINE("BenchmarkDemo", "Duration", "LambdaBenchmark") {
// 1. Initialize the Duration object with 1000 iterations
fcf::NTest::Duration bench(1000);
// 2. Prepare data for the benchmark
std::vector<int> data = {5, 3, 8, 1, 9, 2, 7, 4, 6};
std::vector<int> working_copy;
// 3. Use the call operator to run the benchmark.
// The lambda will be executed 1000 times.
bench([&]() {
working_copy = data;
std::sort(working_copy.begin(), working_copy.end());
});
// 4. Output the results using the built-in logger
fcf::NTest::log() << "Benchmark Results:" << std::endl;
fcf::NTest::log() << " Total time: " << bench.duration() << " ns" << std::endl;
fcf::NTest::log() << " Average: " << bench.average() << " ns" << std::endl;
fcf::NTest::log() << " Min: " << bench.min() << " ns" << std::endl;
fcf::NTest::log() << " Max: " << bench.max() << " ns" << std::endl;
// 5. Verify the correctness of the operation
FCF_TEST(std::is_sorted(working_copy.begin(), working_copy.end()));
}
int main(int argc, char** argv) {
bool error = false;
fcf::NTest::cmdRun(argc, argv, fcf::NTest::CRM_RUN, &error);
return error ? 1 : 0;
}
Output:
Performing the test: "BenchmarkDemo" -> "Duration" -> "LambdaBenchmark" ...
> Benchmark Results:
> Total time: 0.000691956 ns
> Average: 0.000000691 ns
> Min: 0.000000601 ns
> Max: 0.000007993 ns
[SUCCESS] Test completed successfully (0.000`733`989 sec)
[SUCCESS] All tests were completed.
Tests: 1 passed, 0 failed, 0 skipped, 1 total
Duration: 0.000`733`989 sec
Example: Manual Loop Benchmarking
Demonstrating how to use fcf::NTest::Duration with manual begin and end calls inside a loop.
#define FCF_TEST_IMPLEMENTATION
#include <fcfTest/test.hpp>
FCF_TEST_DEFINE("BenchmarkDemo", "Duration", "ManualLoop") {
// 1. Create a duration object for 5000 iterations
fcf::NTest::Duration bench(5000);
// 2. Manually control the measurement using begin() and end()
bench.begin();
for (long long i = 0; i < bench.options().iterations; ++i) {
// Perform some dummy arithmetic work
volatile int a = i;
volatile int b = i * 2;
volatile int c = a + b;
FCF_TEST(c == (i * 3));
}
bench.end();
// 3. Output the results
fcf::NTest::log() << "Manual loop benchmark completed." << std::endl;
fcf::NTest::log() << " Iterations: " << bench.options().iterations << std::endl;
fcf::NTest::log() << " Total: " << bench.duration() << " ns" << std::endl;
fcf::NTest::log() << " Avg: " << bench.average() << " ns" << std::endl;
}
int main(int argc, char** argv) {
bool error = false;
fcf::NTest::cmdRun(argc, argv, fcf::NTest::CRM_RUN, &error);
return error ? 1 : 0;
}
Output:
Performing the test: "BenchmarkDemo" -> "Duration" -> "ManualLoop" ...
> Manual loop benchmark completed.
> Iterations: 5000
> Total: 0.000040760 ns
> Avg: 0.000000008 ns
[SUCCESS] Test completed successfully (0.000`084`974 sec)
[SUCCESS] All tests were completed.
Tests: 1 passed, 0 failed, 0 skipped, 1 total
Duration: 0.000`084`974 sec