holoscan::Timer

Beta
View as Markdown

Utility class to measure time.

This class is used to measure time. It can be used to measure the time between two points in the code, or to measure the time of a function.

The class can be used in two ways:

  • Using the start() and stop() methods to measure the time between two points in the code.
  • Using the constructor with the auto_start parameter set to true to measure the time of a function.

The class can also be used to print the time elapsed between two points in the code, or the time elapsed to execute a function.

The class can be used in two ways:

  • Using the print() method to print the time elapsed between two points in the code.
  • Using the constructor with the auto_output parameter set to true to print the time elapsed to execute a function.

Examples:

#include <holoscan/timer.hpp>

Example

#include <holoscan/utils/timer.hpp>
void foo() {
holoscan::Timer timer("foo() took {:.8f} seconds\n");
// Do something
}
...
void foo() {
holoscan::Timer timer("bar() took {:.8f} seconds\n", true, false);
bar();
timer.stop();
timer.print();
return 0;
}
void foo() {
holoscan::Timer timer("", true, false);
bar();
double elapsed_time = timer.stop();
fmt::print(stderr, "bar() took {:.8f} seconds", elapsed_time);

Constructors

Timer

holoscan::Timer::Timer(holoscan::Timer::Timer(
const char *message,
bool auto_start = true,
bool auto_output = true
)

Construct a new Timer object.

The message parameter is used to print the message when the print() method is called or when the auto_output parameter is set to true and the destructor is called. The first parameter {} in the message will be replaced by the time elapsed.

auto_start is used to start the timer when the constructor is called. auto_output is used to print the time elapsed when the destructor is called. By default, auto_start and auto_output are set to true.

Parameters

message
const char *

The message to print when the timer is stopped.

auto_start
boolDefaults to true

If true, the timer is started when the constructor is called.

auto_output
boolDefaults to true

If true, the time elapsed is printed when the destructor is called.

Example

Timer timer("Time elapsed for foo() method: {:.8f} seconds\n", true, false);
foo();
timer.stop();
timer.print();

Destructor

~Timer

holoscan::Timer::~Timer()holoscan::Timer::~Timer()

Destroy the Timer object.

If the auto_output parameter is set to true, the time elapsed is printed when the destructor is called.


Methods

start

void holoscan::Timer::start()

Start the timer.

stop

double holoscan::Timer::stop()

Stop the timer.

Returns: The time elapsed in seconds.

elapsed_time

double holoscan::Timer::elapsed_time()

Return the time elapsed in seconds.

Returns: The time elapsed in seconds.

print

void holoscan::Timer::print(
const char *message = nullptr
)

Print the time elapsed.

The message passed to the constructor is printed with the time elapsed if no message parameter is passed to the print() method.

The first parameter {} in the message will be replaced by the time elapsed.

Parameters

message
const char *Defaults to nullptr

The message to print.


Member variables

NameTypeDescription
message_const char *
is_auto_output_bool
elapsed_seconds_double
start_std::chrono::time_point< std::chrono::system_clock >
end_std::chrono::time_point< std::chrono::system_clock >