FCF 2.0 development in progress...
> > > > > >
[News] [C++ Libraries API] [C++ Downloads] [Donate to the project] [Contacts]

lastDuration() method from fcf::NTest::Duration class

std::chrono::nanoseconds lastDuration()

Class: fcf::NTest::Duration

Package: fcfTest

File: test.hpp

Available from version: 1.1.7

Calculates the average duration of a single iteration within the last active execution segment.

The lastDuration method provides the average time taken by a single iteration during the most recent execution segment. A segment is defined as the interval between the last resume (or begin) and the last end call.

The calculation is performed as: lastTotalDuration() / iterationCount(). This is particularly useful when benchmarking code that runs in a loop, as it allows you to isolate the performance of a specific batch of iterations from the total accumulated time.

Result
std::chrono::nanoseconds
- The average duration of one iteration within the last segment in nanoseconds.

Example: Average iteration time in a specific segment

Demonstrating how to use lastDuration to find the average time per iteration for a specific batch of work, excluding previous segments.

#define FCF_TEST_IMPLEMENTATION #include <fcfTest/test.hpp> #include <iostream> FCF_TEST_DECLARE("Benchmark", "Average", "LastDurationTest") { // Create a duration object for 1000 iterations fcf::NTest::Duration bench(1000); // 1. First segment: Warm-up (not measured for average) bench.begin(); for(int i = 0; i < 1000; ++i) { /* warm-up work */ } bench.end(); // 2. Second segment: Actual measurement bench.resume(); for(volatile int i = 0; i < 1000; ++i) { // Perform actual work volatile int a = i * 2; volatile int b = i + 5; volatile int c = a + b; FCF_TEST(c == (i * 2 + i + 5)); } bench.end(); // Retrieve the average duration of a single iteration in the LAST segment std::chrono::nanoseconds avgLast = bench.lastDuration(); fcf::NTest::log() << "Average time per iteration in last segment: " << avgLast.count() << " ns" << std::endl; FCF_TEST(avgLast.count() > 0, avgLast.count()); } 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" -> "LastDurationTest" ... > Average time per iteration in last segment: 8 ns [SUCCESS] Test completed successfully (0.000`025`810 sec) [SUCCESS] All tests were completed. Tests: 1 passed, 0 failed, 0 skipped, 1 total Duration: 0.000`025`810 sec