TRIQS/TRIQS 4.0.0
Researching Interacting Quantum Systems
Loading...
Searching...
No Matches
timer.hpp
Go to the documentation of this file.
1// Copyright (c) 2013-2018 Commissariat à l'énergie atomique et aux énergies alternatives (CEA)
2// Copyright (c) 2013-2018 Centre national de la recherche scientifique (CNRS)
3// Copyright (c) 2018 Simons Foundation
4// Copyright (c) 2017 Hugo U.R. Strand
5//
6// This program is free software: you can redistribute it and/or modify
7// it under the terms of the GNU General Public License as published by
8// the Free Software Foundation, either version 3 of the License, or
9// (at your option) any later version.
10//
11// This program is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15//
16// You may obtain a copy of the License at
17// https://www.gnu.org/licenses/gpl-3.0.txt
18//
19// Authors: Olivier Parcollet, Hugo U. R. Strand, Nils Wentzell
20
25
26#pragma once
27
28#include <chrono>
29
30namespace triqs::utility {
31
39 class timer {
40 public:
42 using clock_t = std::chrono::high_resolution_clock;
43
44 private:
45 clock_t::time_point start_time;
46 clock_t::duration total_time = clock_t::duration(0);
47 bool running = false;
48
49 public:
51 void start() {
52 running = true;
53 start_time = clock_t::now();
54 }
55
57 void stop() {
58 total_time += clock_t::now() - start_time;
59 running = false;
60 }
61
63 void reset() {
64 total_time = clock_t::duration(0);
65 running = false;
66 }
67
72 [[nodiscard]] bool is_running() const { return running; }
73
78 operator double() const {
79 std::chrono::duration<double> total_time_seconds(total_time);
80 if (is_running()) total_time_seconds += clock_t::now() - start_time;
81 return total_time_seconds.count();
82 }
83 };
84
85} // namespace triqs::utility
Accumulating wall-clock timer based on std::chrono::high_resolution_clock.
Definition timer.hpp:39
void stop()
Stop the timer and add the elapsed interval to the running total.
Definition timer.hpp:57
std::chrono::high_resolution_clock clock_t
Underlying clock type.
Definition timer.hpp:42
void reset()
Reset the accumulated time to zero and put the timer in the stopped state.
Definition timer.hpp:63
void start()
Start (or resume) the timer.
Definition timer.hpp:51
bool is_running() const
Whether the timer is currently running.
Definition timer.hpp:72