Skip to main content

Stopwatch.ixx File

A high-resolution timer based on std::chrono::steady_clock. More...

Included Headers

#include <chrono>

Namespaces Index

namespacehelios
namespaceutil

Utility functions and helper classes. More...

namespacetime

High-resolution timing helpers (Stopwatch, timers). More...

Classes Index

classStopwatch

A high-resolution timer based on std::chrono::steady_clock. More...

Description

A high-resolution timer based on std::chrono::steady_clock.

File Listing

The file content with the documentation metadata removed is:

1/**
2 * @file Stopwatch.ixx
3 * @brief A high-resolution timer based on std::chrono::steady_clock.
4 */
5
6module;
7
8#include <chrono>
9
10export module helios.util.time.Stopwatch;
11
12export namespace helios::util::time {
13
14 /**
15 * @brief A high-resolution timer based on std::chrono::steady_clock.
16 *
17 * Used to measure elapsed time intervals with the highest precision
18 * provided by the OS (suitable for frame time measurements and profiling).
19 */
20 class Stopwatch {
21
22 private:
23 // Initialize to now so elapsed() is well-defined even if start() wasn't called explicitly.
24 std::chrono::time_point<std::chrono::steady_clock> start_{std::chrono::steady_clock::now()};
25
26 public:
27 /**
28 * @brief Constructs and starts the stopwatch.
29 *
30 * The stopwatch is initialized to the current time so queries are
31 * well-defined immediately after construction.
32 */
33 Stopwatch() noexcept = default;
34
35 /**
36 * @brief Starts (or restarts) the timer.
37 * Captures the current timestamp.
38 */
39 void start() noexcept {
40 start_ = std::chrono::steady_clock::now();
41 }
42
43 /**
44 * @brief Returns the time elapsed since start() was called.
45 *
46 * @return The elapsed time in milliseconds as a floating point value.
47 */
48 [[nodiscard]] float elapsedMs() const noexcept {
49 auto end = std::chrono::steady_clock::now();
50 // Wichtig: Expliziter Cast zu float Millisekunden
51 std::chrono::duration<float, std::milli> duration = end - start_;
52 return duration.count();
53 }
54
55 /**
56 * @brief Returns the time elapsed in seconds.
57 *
58 * @return The elapsed time in seconds as a floating point value.
59 */
60 [[nodiscard]] float elapsedSeconds() const noexcept {
61 auto end = std::chrono::steady_clock::now();
62 std::chrono::duration<float> duration = end - start_;
63 return duration.count();
64 }
65
66 };
67}

Generated via doxygen2docusaurus 2.0.0 by Doxygen 1.15.0.