Timer
(utilities/timer.hpp)
- View this file on GitHub
- Last update: 2026-06-15 23:35:14+09:00
- Include:
#include "utilities/timer.hpp"
Overview
A small wrapper around std::chrono::steady_clock for measuring elapsed time during local tests, heuristics, or randomized algorithms.
Methods
| Method | Description | Complexity |
|---|---|---|
Timer(double limit_seconds = 0.0) |
Starts the timer and stores an optional time limit in seconds. | $O(1)$ |
void reset() |
Restarts the timer from the current time. | $O(1)$ |
void set_limit(double limit_seconds) |
Updates the time limit. | $O(1)$ |
double elapsed() const |
Returns elapsed seconds as a floating-point value. | $O(1)$ |
long long elapsed_ms() const |
Returns elapsed milliseconds. | $O(1)$ |
bool expired() const |
Returns whether elapsed() >= limit_seconds. |
$O(1)$ |
Example
#include "utilities/timer.hpp"
int main() {
m1une::utilities::Timer timer(1.9);
while (!timer.expired()) {
// Do local search, random trials, or stress testing.
}
}
Verified with
Code
#ifndef M1UNE_TIMER_HPP
#define M1UNE_TIMER_HPP 1
#include <chrono>
namespace m1une {
namespace utilities {
struct Timer {
private:
using clock = std::chrono::steady_clock;
clock::time_point _start;
double _limit;
public:
explicit Timer(double limit_seconds = 0.0) : _start(clock::now()), _limit(limit_seconds) {}
void reset() {
_start = clock::now();
}
void set_limit(double limit_seconds) {
_limit = limit_seconds;
}
double elapsed() const {
return std::chrono::duration<double>(clock::now() - _start).count();
}
long long elapsed_ms() const {
return std::chrono::duration_cast<std::chrono::milliseconds>(clock::now() - _start).count();
}
bool expired() const {
return elapsed() >= _limit;
}
};
} // namespace utilities
} // namespace m1une
#endif // M1UNE_TIMER_HPP#line 1 "utilities/timer.hpp"
#include <chrono>
namespace m1une {
namespace utilities {
struct Timer {
private:
using clock = std::chrono::steady_clock;
clock::time_point _start;
double _limit;
public:
explicit Timer(double limit_seconds = 0.0) : _start(clock::now()), _limit(limit_seconds) {}
void reset() {
_start = clock::now();
}
void set_limit(double limit_seconds) {
_limit = limit_seconds;
}
double elapsed() const {
return std::chrono::duration<double>(clock::now() - _start).count();
}
long long elapsed_ms() const {
return std::chrono::duration_cast<std::chrono::milliseconds>(clock::now() - _start).count();
}
bool expired() const {
return elapsed() >= _limit;
}
};
} // namespace utilities
} // namespace m1une