Line data Source code
1 : // Author(s): Maurice Laveaux 2 : // Copyright: see the accompanying file COPYING or copy at 3 : // https://github.com/mCRL2org/mCRL2/blob/master/COPYING 4 : // 5 : // Distributed under the Boost Software License, Version 1.0. 6 : // (See accompanying file LICENSE_1_0.txt or copy at 7 : // http://www.boost.org/LICENSE_1_0.txt) 8 : // 9 : 10 : #ifndef MCRL2_UTILITIES_STOPWATCH_H_ 11 : #define MCRL2_UTILITIES_STOPWATCH_H_ 12 : 13 : #include <chrono> 14 : 15 : /// \brief Implements a simple stopwatch that starts on construction. 16 : class stopwatch 17 : { 18 : public: 19 5 : stopwatch() 20 5 : { 21 5 : reset(); 22 5 : } 23 : 24 : /// \brief Reset the stopwatch to count from this moment onwards. 25 10 : void reset() 26 : { 27 10 : m_timestamp = std::chrono::steady_clock::now(); 28 10 : } 29 : 30 : /// \returns The time in milliseconds since the last reset. 31 6 : long long time() 32 : { 33 6 : return std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - m_timestamp).count(); 34 : } 35 : 36 : /// \returns The time in seconds since the last reset. 37 : double seconds() 38 : { 39 : return std::chrono::duration<double>(std::chrono::steady_clock::now() - m_timestamp).count(); 40 : } 41 : 42 : private: 43 : std::chrono::time_point<std::chrono::steady_clock> m_timestamp; 44 : }; 45 : 46 : #endif // MCRL2_UTILITIES_STOPWATCH_H_