Mantid
Loading...
Searching...
No Matches
CPUTimer.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 +
8#include <ctime>
9#include <iomanip>
10#include <sstream>
11
12namespace Mantid::Kernel {
13
14//----------------------------------------------------------------------------------------------
18 // Record the starting time
19 reset();
20}
21
24#ifdef _WIN32
25#else /* linux & mac */
26 m_start = clock();
27#endif
29}
30
36float CPUTimer::elapsedCPU(bool doReset) {
37 float retval = 0;
38#ifdef _WIN32
39 UNUSED_ARG(doReset);
40#else /* linux & mac */
41 clock_t end = clock();
42 retval = (static_cast<float>(end - m_start)) / CLOCKS_PER_SEC;
43 if (doReset)
44 this->reset();
45#endif
46 return retval;
47}
48
54float CPUTimer::elapsedWallClock(bool doReset) {
55 float retVal = m_wallClockTime.elapsed(false);
56 if (doReset)
57 this->reset();
58 return retVal;
59}
60
67float CPUTimer::CPUfraction(bool doReset) {
68 // Get the wall-clock time without resetting.
69 double wallTime = m_wallClockTime.elapsed(false);
70 double cpuTime = elapsedCPU(false);
71 if (doReset)
72 this->reset();
73 return static_cast<float>((cpuTime / wallTime));
74}
75
77std::string CPUTimer::str() {
78 std::stringstream buffer;
79 buffer << std::fixed << std::setw(7) << std::setprecision(4) << m_wallClockTime.elapsed_no_reset() << " s, CPU "
80 << std::setprecision(2) << this->CPUfraction(false);
81 this->reset();
82 return buffer.str();
83}
84
86std::ostream &operator<<(std::ostream &out, CPUTimer &obj) {
87 out << obj.str();
88 return out;
89}
90
91} // namespace Mantid::Kernel
#define UNUSED_ARG(x)
Function arguments are sometimes unused in certain implmentations but are required for documentation ...
Definition: System.h:64
double obj
the value of the quadratic function
CPUTimer : Timer that uses the CPU time, rather than wall-clock time to measure execution time.
Definition: CPUTimer.h:24
void reset()
Explicitly reset the timer.
Definition: CPUTimer.cpp:23
std::string str()
Convert the elapsed time (without reseting) to a string.
Definition: CPUTimer.cpp:77
CPUTimer()
Constructor.
Definition: CPUTimer.cpp:17
float CPUfraction(bool doReset=true)
Return the fraction of the CPU used (CPUTime/wall-clock time).
Definition: CPUTimer.cpp:67
float elapsedCPU(bool doReset=true)
Calculate the elapsed CPU time, reseting the timer if specified.
Definition: CPUTimer.cpp:36
float elapsedWallClock(bool doReset=true)
Calculate the elapsed wall-clock time, reseting the timer if specified.
Definition: CPUTimer.cpp:54
Timer m_wallClockTime
The regular (wall-clock time).
Definition: CPUTimer.h:38
clock_t m_start
The starting time (implementation dependent format)
Definition: CPUTimer.h:35
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
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
MANTID_KERNEL_DLL std::ostream & operator<<(std::ostream &, CPUTimer &)
Convenience function to provide for easier debug printing.
Definition: CPUTimer.cpp:86