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

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 fcf::NTest::Duration class provides a lightweight interface for measuring the performance of code blocks using std::chrono::steady_clock. It is designed for both manual measurement of arbitrary code segments and automated benchmarking of callable objects (functors) over multiple iterations.

Methods
[CONSTRUCTOR] Duration()
[CONSTRUCTOR] Duration(unsigned long long a_iterations)
- Initializes a duration object with a specific number of iterations for benchmarking
template <typename TFunctor>
void operator()(TFunctor&& a_functor)
- Executes a callable object multiple times and measures the total execution time
unsigned long long iterationCount() const
- Returns the number of iterations configured for the current benchmarking session
void begin()
- Records the current timestamp using a high-resolution clock to mark the start of a timing interval
void resume()
- Resumes the time measurement after a pause, adjusting the global start time to exclude the paused duration.
void end()
- Records the current timestamp using a high-resolution clock to mark the end of a timing interval.
std::chrono::nanoseconds duration() const
- Returns the average duration of a single iteration in nanoseconds
std::string durationStr(bool a_friendly = false) const
- Returns a formatted string representation of the average duration of a single iteration.
std::chrono::nanoseconds totalDuration() const
- Returns the total elapsed time of all benchmark iterations in nanoseconds
std::string totalDurationStr(bool a_friendly = false) const
- Returns a formatted string representation of the total accumulated duration of the last active execution segment.
std::chrono::nanoseconds lastDuration() const
- Calculates the average duration of a single iteration within the last active execution segment.
std::string lastDurationStr(bool a_friendly = false) const
- Returns a formatted string representation of the average duration of a single iteration within the last active execution segment.
std::chrono::nanoseconds lastTotalDuration() const
- Returns the duration of the last active execution segment in nanoseconds.
std::string lastTotalDurationStr(bool a_friendly = false) const
- 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