UTL

Collection of self-contained header-only libraries for C++17

View on GitHub

utl::timer

<- to README.md

<- to implementation.hpp

timer contains a number of methods for time measurement. Intended mainly for measuring code execution time without std::chrono boilerplate. Outputs time as a string, prints formatted local time and date.

Definitions

// Time measurement
void start(); // starts measurement

double elapsed_ms();
double elapsed_sec();
double elapsed_min();
double elapsed_hours();

std::string elapsed_string_ms();
std::string elapsed_string_sec();
std::string elapsed_string_min();
std::string elapsed_string_hours();

std::string elapsed_string_fullform(); // format: "%H hours %M min %S sec %MS ms"

// Local datetime
std::string datetime_string();    // format: "%y-%m-%d %H:%M:%S"
std::string datetime_string_id(); // format: "%y-%m-%d-%H-%M-%S", works in filenames

Methods

Time measurement

timer::start();

Sets internal start timepoint for elapsed measurements.

double timer::elapsed_ms();
double timer::elapsed_sec();
double timer::elapsed_min();
double timer::elapsed_hours();

Returns elapsed time as double. Internally time is measured in nanoseconds.

std::string timer::elapsed_string_ms();
std::string timer::elapsed_string_sec();
std::string timer::elapsed_string_min();
std::string timer::elapsed_string_hours();

Returns elapsed time as std::string with units.

std::string timer::elapsed_string_fullform();

Returns elapsed time in format %H hours %M min %S sec %MS ms.

Datetime

std::string timer::datetime_string();
std::string timer::datetime_string_id();

Returns current local date and time in format %y-%m-%d %H:%M:%S or %y-%m-%d-%H-%M-%S. Since first format is contains characters illegal in filenames, second format can be used instead.

Examples

Measuring time

[ Run this code ]

using namespace utl;

timer::start();

std::this_thread::sleep_for(std::chrono::milliseconds(3700));

std::cout
    << "Time elapsed during sleep_for(3700 ms):\n"
    << timer::elapsed_string_sec() << "\n"
    << timer::elapsed_string_fullform() << "\n";

Output:

Time elapsed during sleep_for(3700 ms):
3.711095 sec
0 hours 0 min 3 sec 712 ms

Getting local datetime

[ Run this code ]

using namespace utl;

std::cout << "Current time is: " << timer::datetime_string() << "\n";

Output:

Current time is: 2023-12-05 02:11:34