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

totalDurationStr() method from fcf::NTest::Duration class

std::string totalDurationStr(bool a_friendly = false)

Class: fcf::NTest::Duration

Package: fcfTest

File: test.hpp

Available from version: 1.1.7

Returns a formatted string representation of the total accumulated duration of the last active execution segment.

The totalDurationStr method provides a human-readable string representation of the total time elapsed during the most recent execution segment (the interval between the last resume or begin call and the last end call).

The output format is determined by the a_friendly parameter:

  • If a_friendly is true (default), the string follows the format: SECONDS.MILLIS`MICROS`NANOS (e.g., 0.001`020`030).
  • If a_friendly is false, the string follows a raw floating-point format with nanosecond precision (e.g., 0.001020030).

Arguments

bool a_friendly = false
- If true, returns a human-friendly format using backticks as separators. If false, returns a raw floating-point format with nanosecond precision.
Result
std::string
- A formatted string representing the total duration of the last active segment.

Example: Demonstrating friendly and raw duration formats

This example shows how to use totalDurationStr to obtain both a human-readable (friendly) and a raw numeric string representation of the time spent in a code block.

#define FCF_TEST_IMPLEMENTATION #include <fcfTest/test.hpp> #include <iostream> #include <vector> #include <algorithm> FCF_TEST_DECLARE("Benchmark", "Format", "DurationStringTest") { // We want to measure the time for 1000 iterations fcf::NTest::Duration bench(1000); // We use the call operator to run the benchmark bench([]() { std::vector<int> v = {5, 3, 8, 1, 9}; (v.begin(), v.end()); }); // 1. Get the friendly string (default behavior) // Format: SECONDS.MILLIS`MICROS`NANOS std::string friendlyStr = bench.totalDurationStr(true); // 2. Get the raw string (non-friendly) // Format: SECONDS.NANOSECONDS (floating point) std::string rawStr = bench.totalDurationStr(false); // Output the results to the console fcf::NTest::log() << "Friendly format: " << friendlyStr << std::endl; fcf::NTest::log() << "Raw format: " << rawStr << std::endl; // Verify the sorting was successful FCF_TEST(true); } 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: "Benchmark" -> "Format" -> "DurationStringTest" ... > Friendly format: 0.000`000`057 > Raw format: 0.000000057 [SUCCESS] Test completed successfully (0.000`015`957 sec) [SUCCESS] All tests were completed. Tests: 1 passed, 0 failed, 0 skipped, 1 total Duration: 0.000`015`957 sec