Class: fcf::NTest::Duration
Package: fcfTest
File: test.hpp
Available from version: 1.1.7
Returns the duration of the last active execution segment in nanoseconds.
The lastTotalDuration method is used to retrieve the duration of the most recent execution segment. A segment is defined as the time interval between the last call to resume (or begin) and the last call to end.
If the timer is currently active (i.e., the last call was begin or end has not been called yet), the method returns the time elapsed since the start of the current segment.
Result
std::chrono::nanoseconds
- The duration of the last active segment in nanoseconds.
Example: Measuring segments with pause/resume
Demonstrating how to use lastTotalDuration to measure specific parts of a process by pausing and resuming the timer.
#define FCF_TEST_IMPLEMENTATION
#include <fcfTest/test.hpp>
#include <iostream>
FCF_TEST_DECLARE("Benchmark", "Segments", "LastSegmentTest") {
// Create a duration object for 1 iteration
fcf::NTest::Duration bench(1);
// 1. Start the timer
bench.begin();
// Simulate first work segment
for(int i = 0; i < 1000000; ++i) { /* dummy work */ }
// 2. Pause the timer
bench.end();
// Simulate some non-measured overhead (e.g., logging or setup)
for(int i = 0; i < 500000; ++i) { /* dummy work */ }
// 3. Resume the timer for the second segment
bench.resume();
// Simulate second work segment
for(int i = 0; i < 1000000; ++i) { /* dummy work */ }
// 4. End the second segment
bench.end();
// Retrieve the duration of the LAST segment only
std::chrono::nanoseconds lastSeg = bench.lastTotalDuration();
fcf::NTest::log() << "Last segment duration: " << lastSeg.count() << " ns" << std::endl;
FCF_TEST(lastSeg.count() > 0);
}
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" -> "Segments" -> "LastSegmentTest" ...
> Last segment duration: 34 ns
[SUCCESS] Test completed successfully (0.000`009`497 sec)
[SUCCESS] All tests were completed.
Tests: 1 passed, 0 failed, 0 skipped, 1 total
Duration: 0.000`009`497 sec