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

fcf::NTest::SteadyClock class

Type:
class fcf::NTest::SteadyClock

Package: fcfTest

File: test.hpp ( link_to_the_text_position_in_the_file )

Available from version: 1.2.5

A high-resolution clock implementation that provides current time points in nanoseconds using std::chrono::steady_clock.

The fcf::NTest::SteadyClock class is a lightweight wrapper around std::chrono::steady_clock. It is designed to be used as a clock source for benchmarking utilities, such as fcf::NTest::DurationBasic.

When the clock is invoked, it returns the current time point as an unsigned long long representing the total number of nanoseconds since the epoch. Because it uses a steady clock, it is monotonic and not subject to adjustments from system time updates, making it ideal for precise duration measurements.

Methods
unsigned long long operator()() const
- Returns the current time point in nanoseconds.

Example: Manual Time Measurement

Demonstrating how to use fcf::NTest::SteadyClock to manually capture high-resolution time points in nanoseconds.

#define FCF_TEST_IMPLEMENTATION #include <fcfTest/test.hpp> #include <iostream> FCF_TEST_DEFINE("ClockDemo", "Utility", "SteadyClockUsage") { // 1. Instantiate the SteadyClock object fcf::NTest::SteadyClock clock; // 2. Capture the starting time point unsigned long long start = clock(); // 3. Perform some work (simulated by a small loop) for (volatile int i = 0; i < 1000000; ++i) { ; } // 4. Capture the ending time point unsigned long long end = clock(); // 5. Calculate the elapsed time in nanoseconds unsigned long long elapsed = end - start; // 6. Output the results using the logger fcf::NTest::log() << "Time measurement completed." << std::endl; fcf::NTest::log() << " Start: " << start << " ns" << std::endl; fcf::NTest::log() << " End: " << end << " ns" << std::endl; fcf::NTest::log() << " Elapsed: " << elapsed << " ns" << std::endl; // 7. Verify the result FCF_TEST(elapsed > 0, "Elapsed time should be greater than zero"); } int main(int argc, char** argv) { bool error = false; fcf::NTest::cmdRun(argc, argv, fcf::NTest::CRM_RUN, &error); return error ? 1 : 0; }

Output:

Performing the test: "ClockDemo" -> "Utility" -> "SteadyClockUsage" ... > Time measurement completed. > Start: 36083838244416 ns > End: 36083840385850 ns > Elapsed: 2141434 ns [SUCCESS] Test completed successfully (0.002`172`063 sec) [SUCCESS] All tests were completed. Tests: 1 passed, 0 failed, 0 skipped, 1 total Duration: 0.002`172`063 sec