Mantid
Loading...
Searching...
No Matches
Timer.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source,
5// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6// SPDX - License - Identifier: GPL - 3.0 +
7//----------------------------------------------------------------------
8// Includes
9//----------------------------------------------------------------------
10#include "MantidKernel/Timer.h"
11#include <chrono>
12#include <ostream>
13#include <sstream>
14
15namespace Mantid::Kernel {
16
20Timer::Timer() : m_start(std::chrono::high_resolution_clock::now()) {}
21
28float Timer::elapsed(bool reset) {
29 float retval = elapsed_no_reset();
30 if (reset)
31 this->reset();
32 return retval;
33}
34
41 const auto now = std::chrono::high_resolution_clock::now();
42 std::chrono::duration<float> duration = now - m_start;
43
44 return duration.count();
45}
46
48void Timer::reset() { m_start = std::chrono::high_resolution_clock::now(); }
49
51std::string Timer::str() const {
52 std::stringstream buffer;
53 buffer << this->elapsed_no_reset() << " sec";
54 return buffer.str();
55}
56
58std::ostream &operator<<(std::ostream &out, const Timer &obj) {
59 out << obj.str();
60 return out;
61}
62
63//------------------------------------------------------------------------
68CodeBlockTimer::CodeBlockTimer(const std::string &name, std::ostream &out)
69 : m_name(name), m_out(out), m_start(std::chrono::system_clock::now()) {}
70
74 const auto stop = std::chrono::system_clock::now();
75 const std::chrono::duration<double> elapsed = stop - m_start;
76 m_out << "Elapsed time (sec) in \"" << m_name << "\": " << elapsed.count() << '\n';
77}
78
79//------------------------------------------------------------------------
84 : m_accumulator(accumulator), m_start(std::chrono::system_clock::now()) {}
85
89 const auto stop = std::chrono::system_clock::now();
90 const std::chrono::duration<double> elapsed = stop - m_start;
91 m_accumulator.increment(elapsed.count());
92}
93
94//------------------------------------------------------------------------
99
103 m_elapsed_sec = 0.0;
104 m_number_of_entrances = 0;
105}
106
111 m_elapsed_sec += time_sec;
112 m_number_of_entrances++;
113}
114
118double CodeBlockMultipleTimer::TimeAccumulator::getElapsed() const { return m_elapsed_sec; }
119
123size_t CodeBlockMultipleTimer::TimeAccumulator::getNumberOfEntrances() const { return m_number_of_entrances; }
124
129 std::ostringstream out;
130 out << "Elapsed time (sec) in \"" << m_name << "\": " << m_elapsed_sec
131 << "; Number of entrances: " << m_number_of_entrances;
132 return out.str();
133}
134
140MANTID_KERNEL_DLL std::ostream &operator<<(std::ostream &out, const CodeBlockMultipleTimer::TimeAccumulator &ta) {
141 out << ta.toString();
142 return out;
143}
144} // namespace Mantid::Kernel
std::string name
Definition Run.cpp:60
double obj
the value of the quadratic function
std::string toString() const
Return the timing summary as a string.
Definition Timer.cpp:128
void reset()
Reset the elapsed wall-clock time and number of times the code block was entered.
Definition Timer.cpp:102
size_t getNumberOfEntrances() const
Return the number of times the code block was entered.
Definition Timer.cpp:123
void increment(const double time_sec)
Increment the elapsed wall-clock time and number of times the code block was entered.
Definition Timer.cpp:110
double getElapsed() const
Return the total elapsed wall-clock time.
Definition Timer.cpp:118
~CodeBlockMultipleTimer()
Calculate the elapsed wall-clock time (seconds) and update the time accumulator.
Definition Timer.cpp:88
TimeAccumulator & m_accumulator
Definition Timer.h:81
std::chrono::time_point< std::chrono::system_clock > m_start
Definition Timer.h:82
~CodeBlockTimer()
Calculate and output to a stream the elapsed wall-clock time (sec)
Definition Timer.cpp:73
std::chrono::time_point< std::chrono::system_clock > m_start
Definition Timer.h:52
A simple class that provides a wall-clock (not processor time) timer.
Definition Timer.h:27
float elapsed_no_reset() const
Returns the wall-clock time elapsed in seconds since the Timer object's creation, or the last call to...
Definition Timer.cpp:40
Timer()
Constructor.
Definition Timer.cpp:20
float elapsed(bool reset=true)
Returns the wall-clock time elapsed in seconds since the Timer object's creation, or the last call to...
Definition Timer.cpp:28
void reset()
Explicitly reset the timer.
Definition Timer.cpp:48
time_point_ns m_start
The starting time.
Definition Timer.h:38
std::string str() const
Convert the elapsed time (without reseting) to a string.
Definition Timer.cpp:51
MANTID_KERNEL_DLL std::ostream & operator<<(std::ostream &, CPUTimer &)
Convenience function to provide for easier debug printing.
Definition CPUTimer.cpp:86
STL namespace.