VoxelStorm's C++ RAII timer library for one-line function and scope time-taken reporting.
For simple logging of time taken within a function or a scope. Can be combined with LogStorm, or work standalone with std::cout
or other streams.
The simplest usage is as follows:
#include <timestorm/timer.h>
{
timestorm::timer<float> timer();
// do something long-running - time result prints on destruction of timer.
}
This will output something along the lines of Done in 1.2s
. Here float
sets the reporting precision for output.
To set the message to output, use:
timestorm::timer<float> timer("Operation completed in ");
When you want to update the prefix or suffix based on operation success or failure, which may not have been known at the start of timing:
timestorm::timer<float> timer("Operation completed in ");
if(success) {
timer.set_prefix("Operation completed successfully in "); // set the timer prefix to describe success
} else {
timer.set_prefix("Operation failed in "); // set the timer prefix to describe failure
}
std::chrono::time_pointstd::chrono::system_clock time_start = std::chrono::system_clock::now();
std::function<std::string()> prefix; // what to run to generate output when finished before the time value
std::function<std::string()> suffix; // what to run to generate output when finished after the time value
timescale scale = timescale::AUTO; // on what timescale to report the results
timer(timescale new_scale = timescale::AUTO,
std::string const &message_pre = "Done in ",
std::string const &message_post = ".\n");
timer(std::string const &message_pre = "Done in ",
std::string const &message_post = ".\n");
timer(timescale new_scale = timescale::AUTO,
std::function<std::string()> const &function_pre = []{return "Done in ";},
std::function<std::string()> const &function_post = []{return ".\n";});
timer(std::function<std::string()> const &function_pre = []{return "Done in ";},
std::function<std::string()> const &function_post = []{return ".\n";});
timer(sink_t &sink,
timescale new_scale = timescale::AUTO,
std::string const &message_pre = "Done in ",
std::string const &message_post = ".\n");
timer(sink_t &sink,
std::string const &message_pre = "Done in ",
std::string const &message_post = ".\n");
timer(sink_t &sink,
timescale new_scale = timescale::AUTO,
std::function<std::string()> const &function_pre = []{return "Done in ";},
std::function<std::string()> const &function_post = []{return ".\n";});
timer(sink_t &sink,
std::function<std::string()> const &function_pre = []{return "Done in ";},
std::function<std::string()> const &function_post = []{return ".\n";});
~timer();
void output();
void reset();
T const get_time();
std::string const get_unit();
void set_prefix(std::string const &new_prefix);
void set_suffix(std::string const &new_suffix);
Use with LogStorm
Simply pass the logger you wish to use as the first parameter:
timestorm::timer<float, logstorm::manager> timer(logger, "Operation completed in ", ".");