Class: fcf::NTest::Duration
Package: fcfTest
File: test.hpp
Available from version: 1.1.7
Returns a formatted string representation of the average duration of a single iteration.
The durationStr method provides a human-readable string representation of the average time taken by a single execution of the benchmarked code block.
The average is calculated by dividing the total accumulated duration by the number of iterations configured in the fcf::NTest::Duration object. The output format depends on the a_friendly parameter:
- If a_friendly is true (default), the string follows the format:
SECONDS.MILLIS`MICROS`NANOS (e.g., 0.000`012`345).
- If a_friendly is false, the string follows a raw floating-point format with nanosecond precision (e.g.,
0.000012345).
Arguments
bool a_friendly = false
- If true, returns a human-friendly format using backticks as separators. If false, returns a raw floating-point format with nanosecond precision.
Result
std::string
- A formatted string representing the average duration of one iteration.
Example: Demonstrating average duration string formats
This example demonstrates how to use durationStr to get the average time per iteration in both human-readable and raw formats.
#define FCF_TEST_IMPLEMENTATION
#include <fcfTest/test.hpp>
#include <iostream>
#include <vector>
#include <algorithm>
FCF_TEST_DECLARE("Benchmark", "Average", "DurationStrTest") {
// We set up a benchmark for 5000 iterations
fcf::NTest::Duration bench(5000);
// We use the call operator to run the benchmark
bench([]() {
std::vector<int> v = {10, 5, 8, 1, 7};
(v.begin(), v.end());
});
// 1. Get the friendly average duration string (default)
// Format: SECONDS.MILLIS`MICROS`NANOS
std::string friendlyAvg = bench.durationStr(true);
// 2. Get the raw average duration string (non-friendly)
// Format: SECONDS.NANOSECONDS (floating point)
std::string rawAvg = bench.durationStr(false);
// Output the results to the console
fcf::NTest::log() << "Friendly average: " << friendlyAvg << std::endl;
fcf::NTest::log() << "Raw average: " << rawAvg << std::endl;
// Verify the test passed
FCF_TEST(true);
}
int main(int a_argc, char* a_argv[]) {
bool error = false;
fcf::NTest::cmdRun(a_argc, a_argv, fcf::NTest::CRM_RUN, &error);
return error ? 1 : 0;
}
Output:
Performing the test: "Benchmark" -> "Average" -> "DurationStrTest" ...
> Friendly average: 0.000`001`234
> Raw average: 0.000001234
[SUCCESS] Test completed successfully (0.006`170`000 sec)
[SUCCESS] All tests were completed.
Tests: 1 passed, 0 failed, 0 skipped, 1 total
Duration: 0.006`170`000 sec