Zip and Skip Tries
Loading...
Searching...
No Matches
Public Member Functions | Public Attributes | List of all members
WallTimer Struct Reference

A timer class measuring wall-clock time using std::chrono::high_resolution_clock. More...

#include <data.hpp>

Public Member Functions

 WallTimer ()=default
 Default constructor.
 
size_t elapsed_nanoseconds () const noexcept
 Calculates the elapsed time since the timer was started.
 
void start (const std::string &prompt="", bool reset=true) noexcept
 Starts or resets the timer and optionally prints a starting message.
 
size_t print () noexcept
 Prints the elapsed time since the last start/reset to stdout.
 

Public Attributes

std::chrono::high_resolution_clock::time_point start_time
 

Detailed Description

A timer class measuring wall-clock time using std::chrono::high_resolution_clock.

Provides methods to start the timer, get the elapsed time, and print the elapsed time. Suitable for measuring the real time elapsed, including potential waits or system overhead.

Definition at line 115 of file data.hpp.

Constructor & Destructor Documentation

◆ WallTimer()

WallTimer::WallTimer ( )
default

Default constructor.

Member Function Documentation

◆ elapsed_nanoseconds()

size_t WallTimer::elapsed_nanoseconds ( ) const
inlinenoexcept

Calculates the elapsed time since the timer was started.

Returns
size_t The elapsed time in nanoseconds.

Definition at line 126 of file data.hpp.

127 {
128 auto end = std::chrono::high_resolution_clock::now();
129 auto duration = std::chrono::duration_cast<std::chrono::nanoseconds>(end - start_time);
130 return duration.count();
131 }
T * alloc_to_device(size_t size)
Allocates memory on the CUDA device.
std::chrono::high_resolution_clock::time_point start_time
Definition data.hpp:117

◆ print()

size_t WallTimer::print ( )
inlinenoexcept

Prints the elapsed time since the last start/reset to stdout.

Calculates the elapsed time, formats it using pretty_print, and prints "done (formatted_time)\n".

Returns
size_t The elapsed time in nanoseconds that was printed.

Definition at line 158 of file data.hpp.

159 {
161 printf("done (%s)\n", pretty_print(nanoseconds).c_str());
162 fflush(stdout); // Ensure the output is displayed immediately
163 return nanoseconds;
164 }
std::string pretty_print(size_t nanoseconds)
Converts a duration in nanoseconds to a human-readable string format.
Definition data.cpp:65
size_t elapsed_nanoseconds() const noexcept
Calculates the elapsed time since the timer was started.
Definition data.hpp:126

◆ start()

void WallTimer::start ( const std::string &  prompt = "",
bool  reset = true 
)
inlinenoexcept

Starts or resets the timer and optionally prints a starting message.

Parameters
promptA message to print to stdout indicating what is being timed. If empty, no message is printed.
resetIf true (default), resets the start time. If false, keeps the previous start time (useful for cumulative timing).

Definition at line 138 of file data.hpp.

139 {
140 if (reset)
141 {
142 start_time = std::chrono::high_resolution_clock::now();
143 }
144
145 if (!prompt.empty())
146 {
147 // Use printf for potentially better performance in tight loops compared to std::cout
148 printf("%s... \t", prompt.c_str());
149 fflush(stdout); // Ensure the prompt is displayed immediately
150 }
151 }

Member Data Documentation

◆ start_time

std::chrono::high_resolution_clock::time_point WallTimer::start_time

Stores the time point when the timer was last started/reset.

Definition at line 117 of file data.hpp.


The documentation for this struct was generated from the following file: