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