forked from npshub/mantid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Timer.cpp
63 lines (54 loc) · 1.86 KB
/
Timer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// Mantid Repository : https://github.com/mantidproject/mantid
//
// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
// NScD Oak Ridge National Laboratory, European Spallation Source,
// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
// SPDX - License - Identifier: GPL - 3.0 +
//----------------------------------------------------------------------
// Includes
//----------------------------------------------------------------------
#include "MantidKernel/Timer.h"
#include <chrono>
#include <ostream>
#include <sstream>
namespace Mantid::Kernel {
/** Constructor.
* Instantiating the object starts the timer.
*/
Timer::Timer() { m_start = std::chrono::high_resolution_clock::now(); }
/** Returns the wall-clock time elapsed in seconds since the Timer object's
*creation, or the last call to elapsed
*
* @param reset :: set to true to reset the clock (default)
* @return time in seconds
*/
float Timer::elapsed(bool reset) {
float retval = elapsed_no_reset();
if (reset)
this->reset();
return retval;
}
/** Returns the wall-clock time elapsed in seconds since the Timer object's
*creation, or the last call to elapsed
*
* @return time in seconds
*/
float Timer::elapsed_no_reset() const {
const auto now = std::chrono::high_resolution_clock::now();
std::chrono::duration<float> duration = now - m_start;
return duration.count();
}
/// Explicitly reset the timer.
void Timer::reset() { m_start = std::chrono::high_resolution_clock::now(); }
/// Convert the elapsed time (without reseting) to a string.
std::string Timer::str() const {
std::stringstream buffer;
buffer << this->elapsed_no_reset() << "s";
return buffer.str();
}
/// Convenience function to provide for easier debug printing.
std::ostream &operator<<(std::ostream &out, const Timer &obj) {
out << obj.str();
return out;
}
} // namespace Mantid::Kernel