Type:
class fcf::NTest::TimeDuration
Package: fcfTest
File:
test.hpp
( link_to_the_text_position_in_the_file )
Available from version: 1.2.5
A high-resolution time duration wrapper that provides convenient arithmetic operations and human-readable string formatting for nanosecond-precision timing.
The fcf::NTest::TimeDuration class is a lightweight wrapper around an unsigned 64-bit integer representing time in nanoseconds. It is designed to simplify time-based calculations and formatting within the fcf::NTest::Duration benchmarking framework. The class supports full arithmetic operations (addition, subtraction, multiplication, division) with both other fcf::NTest::TimeDuration objects and standard arithmetic types. Additionally, it provides highly flexible string formatting, allowing users to switch between raw nanosecond precision and a 'friendly' human-readable format (e.g., SEC.MIL`MICROS`NANOS).
Methods
- A class representing a duration of time, stored in nanoseconds.
unsigned long long
count()
const
- Returns the raw duration value in nanoseconds.
std::string
str(
bool a_friendly
= false)
const
- Converts the duration to a string representation.
fcf::NTest::TimeDuration
operator+(
fcf::NTest::TimeDuration a_rhs)
- Adds a duration or an arithmetic value to the current duration.
fcf::NTest::TimeDuration
operator-(
fcf::NTest::TimeDuration a_rhs)
- Subtracts a duration or an arithmetic value from the current duration.
fcf::NTest::TimeDuration
operator/(
fcf::NTest::TimeDuration a_rhs)
- Divides the current duration by a duration or an arithmetic value.
fcf::NTest::TimeDuration
operator*(
fcf::NTest::TimeDuration a_rhs)
- Multiplies the current duration by a duration or an arithmetic value.
bool
operator==(
const fcf::NTest::TimeDuration& a_rhs)
const
- Checks if two durations are equal.
bool
operator!=(
const fcf::NTest::TimeDuration& a_rhs)
const
- Checks if two durations are not equal.
bool
operator<(
const fcf::NTest::TimeDuration& a_rhs)
const
- Checks if a duration is less than another.
bool
operator<=(
const fcf::NTest::TimeDuration& a_rhs)
const
- Checks if a duration is less than or equal to another.
bool
operator>(
const fcf::NTest::TimeDuration& a_rhs)
const
- Checks if a duration is greater than another.
bool
operator>=(
const fcf::NTest::TimeDuration& a_rhs)
const
- Checks if a duration is greater than or equal to another.
- Checks if the duration is zero.
fcf::NTest::TimeDuration&
operator=(
unsigned long long a_seconds)
fcf::NTest::TimeDuration&
operator=(
const fcf::NTest::TimeDuration& a_rhs)
- Assigns a new duration value.
fcf::NTest::TimeDuration&
operator-=(
unsigned long long a_value)
fcf::NTest::TimeDuration&
operator-=(
const fcf::NTest::TimeDuration& a_rhs)
- Subtracts a duration or an arithmetic value from the current duration.
fcf::NTest::TimeDuration&
operator+=(
unsigned long long a_value)
fcf::NTest::TimeDuration&
operator+=(
const fcf::NTest::TimeDuration& a_rhs)
- Adds a duration or an arithmetic value to the current duration.
fcf::NTest::TimeDuration&
operator/=(
unsigned long long a_value)
fcf::NTest::TimeDuration&
operator/=(
const fcf::NTest::TimeDuration& a_rhs)
- Divides the current duration by a duration or an arithmetic value.
fcf::NTest::TimeDuration&
operator*=(
unsigned long long a_value)
fcf::NTest::TimeDuration&
operator*=(
const fcf::NTest::TimeDuration& a_rhs)
- Multiplies the current duration by a duration or an arithmetic value.
std::ostream&
operator<<(
std::ostream& a_stream,
const fcf::NTest::TimeDuration& a_td)
- Inserts the duration into an output stream.
Example: Arithmetic and Formatting
Demonstrates how to perform arithmetic operations with TimeDuration and how to use the friendly string formatting for human-readable output.
#define FCF_TEST_IMPLEMENTATION
#include <fcfTest/test.hpp>
#include <iostream>
FCF_TEST_DEFINE("TimeDemo", "Utils", "ArithmeticTest"){
// 1. Initialize a duration of 1.5 seconds (1,500,000,000 nanoseconds)
fcf::NTest::TimeDuration td(1500000000ULL);
// 2. Perform arithmetic operations
// Adding 500 milliseconds
fcf::NTest::TimeDuration added = td + 500000000ULL;
// Subtracting 100 milliseconds
fcf::NTest::TimeDuration subtracted = td - 100000000ULL;
// 3. Display results using different formats
// Raw format: 1.500000000
fcf::NTest::log() << "Raw: " << added.str(false) << std::endl;
// Friendly format: 2.000`500`000
fcf::NTest::log() << "Friendly: " << subtracted.str(true) << std::endl;
// 4. Assertions
FCF_TEST(added.count() == 2000000000ULL, "Addition failed");
FCF_TEST(subtracted.count() == 1400000000ULL, "Subtraction failed");
}
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: "TimeDemo" -> "Utils" -> "ArithmeticTest" ...
> Raw: 2.000000000
> Friendly: 1.400`000`000
[SUCCESS] Test completed successfully (0.000`013`334 sec)
[SUCCESS] All tests were completed.
Tests: 1 passed, 0 failed, 0 skipped, 1 total
Duration: 0.000`013`334 sec