fcf::NTest::Duration class
Type:
class fcf::NTest::Duration
Package: fcfTest
File: test.hpp
Available from version: 1.0.1
A high-resolution timer utility for benchmarking code execution. It supports both manual timing control and automated execution of functors over a specified number of iterations.
The
Methods
- Executes a callable object multiple times and measures the total execution time
- Returns the number of iterations configured for the current benchmarking session
- Records the current timestamp using a high-resolution clock to mark the start of a timing interval
- Resumes the time measurement after a pause, adjusting the global start time to exclude the paused duration.
- Records the current timestamp using a high-resolution clock to mark the end of a timing interval.
- Returns the average duration of a single iteration in nanoseconds
- Returns a formatted string representation of the average duration of a single iteration.
- Returns the total elapsed time of all benchmark iterations in nanoseconds
- Returns a formatted string representation of the total accumulated duration of the last active execution segment.
- Calculates the average duration of a single iteration within the last active execution segment.
- Returns a formatted string representation of the average duration of a single iteration within the last active execution segment.
- Returns the duration of the last active execution segment in nanoseconds.
- Returns a formatted string representation of the total duration of the last active execution segment.
Example: Automated Benchmarking
The most concise way to measure a lambda or function by passing it directly to the object.
#define FCF_TEST_IMPLEMENTATION
#include <fcfTest/test.hpp>
FCF_TEST_DEFINE ("Library1", "Base", "Main test"){
fcf::NTest::Duration bench (1000);
bench ([]() {
// Code to measure
std::vector<int> v = {3, 1, 2};
std::sort (v.begin(), v.end());
});
fcf::NTest::log() << "Avg: " << bench.durationStr (true) << " sec" << std::endl;
fcf::NTest::log() << "Total: " << bench.totalDurationStr (true) << " sec" << std::endl;
}
int main(int a_argc, char* a_argv[]) {
// Standard execution: Parse and run.
bool error = false;
fcf::NTest::cmdRun (a_argc, a_argv, fcf::NTest::CRM_RUN , &error);
return error ? 1 : 0;
}
Output:
Performing the test: "Library1" -> "Base" -> "Main test" ...
> Avg: 0.000`000`020 sec
> Total: 0.000`020`405 sec
[SUCCESS] Test completed successfully (0.000`036`941 sec)
[SUCCESS] All tests were completed.
Tests: 1 passed, 0 failed, 0 skipped, 1 total
Duration: 0.000`036`941 sec
VPSDime is an industry leading VPS hosting company that provides virtualized server services with high performance, availability and friendly support.