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

A timer class measuring CPU time used by the current process using std::clock(). More...

#include <data.hpp>

Public Member Functions

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

Public Attributes

std::clock_t start_time
 

Detailed Description

A timer class measuring CPU time used by the current process using std::clock().

Provides methods to start the timer, get the elapsed CPU time, and print the elapsed CPU time. Suitable for measuring the amount of processor time consumed by the code, excluding time spent waiting.

Note
The resolution and accuracy of std::clock() can vary between systems.

Definition at line 174 of file data.hpp.

Constructor & Destructor Documentation

◆ CPUTimer()

CPUTimer::CPUTimer ( )
default

Default constructor.

Member Function Documentation

◆ elapsed_nanoseconds()

size_t CPUTimer::elapsed_nanoseconds ( ) const
inlinenoexcept

Calculates the elapsed CPU time since the timer was started.

Returns
size_t The elapsed CPU time in nanoseconds.

Definition at line 185 of file data.hpp.

186 {
187 auto end = std::clock();
188 // Calculate duration in nanoseconds based on clock ticks and CLOCKS_PER_SEC
189 return static_cast<size_t>(static_cast<double>(end - start_time) * 1e9 / CLOCKS_PER_SEC);
190 }
T * alloc_to_device(size_t size)
Allocates memory on the CUDA device.
std::clock_t start_time
Definition data.hpp:176

◆ print()

size_t CPUTimer::print ( )
inlinenoexcept

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

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

Returns
size_t The elapsed CPU time in nanoseconds that was printed.

Definition at line 216 of file data.hpp.

217 {
219 printf("done (%s)\n", pretty_print(nanoseconds).c_str());
220 fflush(stdout); // Ensure the output is displayed immediately
221 return nanoseconds;
222 }
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 CPU time since the timer was started.
Definition data.hpp:185

◆ start()

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

Starts or resets the CPU 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.

Definition at line 197 of file data.hpp.

198 {
199 if (reset)
200 {
201 start_time = std::clock();
202 }
203
204 if (!prompt.empty())
205 {
206 printf("%s... \t", prompt.c_str());
207 fflush(stdout); // Ensure the prompt is displayed immediately
208 }
209 }

Member Data Documentation

◆ start_time

std::clock_t CPUTimer::start_time

Stores the clock tick count when the timer was last started/reset.

Definition at line 176 of file data.hpp.


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