Skip to content

Struct Timer

ClassList > Timer

RAII-based timer for profiling code sections. More...

  • #include <structs.h>

Public Attributes

Type Name
time_t __duration = 0
Cached duration value.
time_t end = 0
Timer end timestamp (clock ticks)
int fact = 1
Multiplication factor for time conversion (1 = ms)
bool is_print
Enable/disable automatic output.
std::string name
Timer name for identification in output.
time_t start = 0
Timer start timestamp (clock ticks)
time_t temp = 0
Previous tick timestamp for delta calculation.
time_t tick = 0
Current tick timestamp (clock ticks)

Public Functions

Type Name
time_t GetDuration ()
Get total elapsed time without stopping timer.
time_t Tick ()
Record intermediate checkpoint and print delta time.
Timer (std::string name="Thread", int fact=1, bool print=true)
Construct timer and start measurement.
~Timer ()
Destructor: stop timer and print total duration.

Detailed Description

Timer automatically starts on construction and reports elapsed time on destruction. Provides intermediate Tick() for checkpoint measurements. Output is conditionally compiled in DEBUG builds only.

Lifecycle: * Construction: Capture start time, print start message * Tick(): Print duration since last tick or start * Destruction: Print total duration from start to end

Timing Precision: * Uses std::clock() for CPU time measurement * Resolution: CLOCKS_PER_SEC (typically microseconds) * Measures CPU time, not wall-clock (excludes I/O wait, etc.)

Note:

Timer measures CPU time, which may differ from real time on multi-core.

Note:

For accurate GPU timing, use glQueryCounter with GL_TIMESTAMP.

Public Attributes Documentation

variable __duration

Cached duration value.

time_t Timer::__duration;


variable end

Timer end timestamp (clock ticks)

time_t Timer::end;


variable fact

Multiplication factor for time conversion (1 = ms)

int Timer::fact;


variable is_print

Enable/disable automatic output.

bool Timer::is_print;


variable name

Timer name for identification in output.

std::string Timer::name;


variable start

Timer start timestamp (clock ticks)

time_t Timer::start;


variable temp

Previous tick timestamp for delta calculation.

time_t Timer::temp;


variable tick

Current tick timestamp (clock ticks)

time_t Timer::tick;


Public Functions Documentation

function GetDuration

Get total elapsed time without stopping timer.

inline time_t Timer::GetDuration () 

Calculates and prints duration from start to current time without destroying the timer. Useful for progress monitoring in long operations.

Returns:

Total duration since construction in clock ticks

Note:

Does not stop timer - can be called multiple times.

Note:

Output only in DEBUG builds when is_print is true.

Example:

Timer t("Batch Processing");
for (int i = 0; i < 1000; i++) {
    ProcessItem(i);
    if (i % 100 == 0) t.GetDuration(); // Print progress
}


function Tick

Record intermediate checkpoint and print delta time.

inline time_t Timer::Tick () 

Captures current time and prints duration since last Tick() or construction. Useful for profiling multiple stages within a single timer scope.

Returns:

Duration since last tick in clock ticks (divide by CLOCKS_PER_SEC for seconds)

Note:

Resets internal delta reference - next Tick() measures from this point.

Note:

Output only in DEBUG builds when is_print is true.

Example:

Timer t("Renderer");
DrawGeometry();
t.Tick();  // Prints geometry pass duration
ApplyPostProcessing();
t.Tick();  // Prints post-processing duration


function Timer

Construct timer and start measurement.

inline Timer::Timer (
    std::string name="Thread",
    int fact=1,
    bool print=true
) 

Captures start time and prints initialization message if is_print is true. Timer begins immediately upon construction (RAII pattern).

Parameters:

  • name Descriptive name for timer (appears in output)
  • fact Time conversion factor (1 = milliseconds, 1000 = seconds)
  • print Enable automatic output (DEBUG builds only)

Note:

Output only appears in DEBUG builds, regardless of is_print.

Example:

{
    Timer t("Mesh Loading");
    LoadMeshFromFile(...);
} // Timer destroyed here, prints total duration


function ~Timer

Destructor: stop timer and print total duration.

inline Timer::~Timer () 

Automatically called when timer goes out of scope. Captures end time and prints total duration if is_print is enabled.

Note:

RAII guarantees timer stop even if exceptions thrown.

Note:

Output only in DEBUG builds when is_print is true.



The documentation for this class was generated from the following file src/extra/structs.h