Newer
Older
// 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
// SPDX - License - Identifier: GPL - 3.0 +
Russell Taylor
committed
//----------------------------------------------------------------------
// Includes
//----------------------------------------------------------------------
#include "MantidKernel/Timer.h"
Peterson, Peter
committed
#include <sstream>
Russell Taylor
committed
namespace Mantid {
namespace Kernel {
Russell Taylor
committed
/** Constructor.
* Instantiating the object starts the timer.
*/
Timer::Timer() { m_start = std::chrono::high_resolution_clock::now(); }
Russell Taylor
committed
/** 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();
Peterson, Peter
committed
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();
Peterson, Peter
committed
}
/// Explicitly reset the timer.
void Timer::reset() { m_start = std::chrono::high_resolution_clock::now(); }
Russell Taylor
committed
Peterson, Peter
committed
/// Convert the elapsed time (without reseting) to a string.
Peterson, Peter
committed
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) {
Peterson, Peter
committed
out << obj.str();
return out;
}
Russell Taylor
committed
} // namespace Kernel
} // namespace Mantid