Debian Codegraph @

src/atlas/runtime/trace/StopWatch.h

raw
1 /*
2 * (C) Copyright 2013 ECMWF.
3 *
4 * This software is licensed under the terms of the Apache Licence Version 2.0
5 * which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
6 * In applying this licence, ECMWF does not waive the privileges and immunities
7 * granted to it by virtue of its status as an intergovernmental organisation
8 * nor does it submit to any jurisdiction.
9 */
10
11 #pragma once
12
13 #include <chrono>
14
15 namespace atlas {
16 namespace runtime {
17 namespace trace {
18
19 //-----------------------------------------------------------------------------------------------------------
20
21 class StopWatch {
22 public:
23 StopWatch();
24 StopWatch(double elapsed);
25 void start();
26 void stop();
27 void reset();
28 double elapsed() const;
29
30 private:
31 std::chrono::duration<double> elapsed_;
32 std::chrono::steady_clock::time_point start_;
33 bool running_{false};
34 };
35
36 //-----------------------------------------------------------------------------------------------------------
37
38 inline StopWatch::StopWatch(): elapsed_(0) {}
39
40 inline StopWatch::StopWatch(double elapsed): elapsed_(elapsed) {}
41
42 inline void StopWatch::start() {
43 start_ = std::chrono::steady_clock::now();
44 running_ = true;
45 }
46
47 inline void StopWatch::stop() {
48 if (running_) {
49 elapsed_ += (std::chrono::steady_clock::now() - start_);
50 running_ = false;
51 }
52 }
53
54 inline void StopWatch::reset() {
55 elapsed_ = std::chrono::seconds::zero();
56 start_ = std::chrono::steady_clock::now();
57 }
58
59 inline double StopWatch::elapsed() const {
60 return elapsed_.count();
61 }
62
63 //-----------------------------------------------------------------------------------------------------------
64
65 } // namespace trace
66 } // namespace runtime
67 } // namespace atlas