> For clean Markdown of any page, append .md to the page URL.
> For a complete documentation index, see https://docs.nvidia.com/holoscan/sdk-user-guide/llms.txt.
> For AI client integration (Claude Code, Cursor, etc.), connect to the MCP server at https://docs.nvidia.com/holoscan/sdk-user-guide/_mcp/server.

# holoscan::Timer

> Utility class to measure time.

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:

```cpp showLineNumbers={false}
#include <holoscan/timer.hpp>
```

## Example

```cpp showLineNumbers={false}
#include <holoscan/utils/timer.hpp>

void foo() {
  holoscan::Timer timer("foo() took {:.8f} seconds\n");
  // Do something
}
...
```

```cpp showLineNumbers={false}
void foo() {
  holoscan::Timer timer("bar() took {:.8f} seconds\n", true, false);
  bar();
  timer.stop();
  timer.print();
  return 0;
}
```

```cpp showLineNumbers={false}
void foo() {
  holoscan::Timer timer("", true, false);
  bar();
  double elapsed_time = timer.stop();
  fmt::print(stderr, "bar() took {:.8f} seconds", elapsed_time);
```

***

## Constructors

### Timer \[#timer]

```cpp showLineNumbers={false}
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**

The message to print when the timer is stopped.

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

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

**Example**

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

### Destructor \[#destructor]

### \~Timer

```cpp showLineNumbers={false}
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 \[#start]

```cpp showLineNumbers={false}
void holoscan::Timer::start()
```

Start the timer.

### stop \[#stop]

```cpp showLineNumbers={false}
double holoscan::Timer::stop()
```

Stop the timer.

**Returns:** The time elapsed in seconds.

### elapsed\_time \[#elapsedtime]

```cpp showLineNumbers={false}
double holoscan::Timer::elapsed_time()
```

Return the time elapsed in seconds.

**Returns:** The time elapsed in seconds.

### print \[#print]

```cpp showLineNumbers={false}
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**

The message to print.

***

## Member variables

| Name               | Type                                                   | Description |
| ------------------ | ------------------------------------------------------ | ----------- |
| `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 >` |             |