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

fcf::NTest::Duration class

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.

Detailed description

The fcf::NTest::Duration class provides a lightweight interface for measuring the performance of code blocks using std::chrono::high_resolution_clock. It is designed for both manual measurement of arbitrary code segments and automated benchmarking of callable objects (functors) over multiple iterations.

Example: Automated Benchmarking

The most concise way to measure a lambda or function by passing it directly to the object.

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() << bench.duration().count() << " ns" << std::endl;

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 iterations()
- 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 end()
- Records the current timestamp using a high-resolution clock to mark the end of a timing interval.
std::chrono::nanoseconds totalDuration()
- Returns the total elapsed time of all benchmark iterations in nanoseconds
std::chrono::nanoseconds duration()
- Returns the average duration of a single iteration in nanoseconds