Skip to content

C++ RAII timer library for one-line function and scope time-taken reporting

License

Notifications You must be signed in to change notification settings

VoxelStorm-Ltd/timestorm

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

27 Commits
 
 
 
 
 
 

Repository files navigation

TimeStorm

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.

Usage

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
}

Public members

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

Functions

  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 ", ".");

About

C++ RAII timer library for one-line function and scope time-taken reporting

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published