GEOSX
Stopwatch.hpp
Go to the documentation of this file.
1 /*
2  * ------------------------------------------------------------------------------------------------------------
3  * SPDX-License-Identifier: LGPL-2.1-only
4  *
5  * Copyright (c) 2018-2020 Lawrence Livermore National Security LLC
6  * Copyright (c) 2018-2020 The Board of Trustees of the Leland Stanford Junior University
7  * Copyright (c) 2018-2020 TotalEnergies
8  * Copyright (c) 2019- GEOSX Contributors
9  * All rights reserved
10  *
11  * See top level LICENSE, COPYRIGHT, CONTRIBUTORS, NOTICE, and ACKNOWLEDGEMENTS files for details.
12  * ------------------------------------------------------------------------------------------------------------
13  */
14 
19 #ifndef GEOS_COMMON_STOPWATCH_HPP
20 #define GEOS_COMMON_STOPWATCH_HPP
21 
22 #include <chrono>
23 
24 namespace geos
25 {
26 
31 class Stopwatch
32 {
33 public:
34 
39  {
40  zero();
41  }
42 
47  Stopwatch( real64 & resultVar )
48  : m_result( &resultVar )
49  {
50  zero();
51  }
52 
57  {
58  if( m_result )
59  {
60  *m_result = elapsedTime();
61  }
62  }
63 
67  void zero()
68  {
69  m_start = std::chrono::steady_clock::now();
70  }
71 
77  {
78  std::chrono::steady_clock::time_point const end = std::chrono::steady_clock::now();
79  std::chrono::duration< real64 > const diff = end - m_start;
80  return diff.count();
81  }
82 
83 private:
84 
86  std::chrono::steady_clock::time_point m_start;
87 
89  real64 * m_result{};
90 };
91 
92 } // end geosx
93 
94 #endif
Class defining a simple stopwatch for interval timing.
Definition: Stopwatch.hpp:32
real64 elapsedTime()
Return elapsed time in seconds since zero() was last called.
Definition: Stopwatch.hpp:76
void zero()
Zero out the timer.
Definition: Stopwatch.hpp:67
Stopwatch()
Constructor.
Definition: Stopwatch.hpp:38
~Stopwatch()
Destructor.
Definition: Stopwatch.hpp:56
Stopwatch(real64 &resultVar)
Constructor.
Definition: Stopwatch.hpp:47
double real64
64-bit floating point type.
Definition: DataTypes.hpp:139