File: timer.hh

package info (click to toggle)
dune-common 2.11.0-1~exp2
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 6,056 kB
  • sloc: cpp: 54,404; python: 4,136; sh: 1,657; makefile: 17
file content (129 lines) | stat: -rw-r--r-- 3,358 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
// vi: set et ts=4 sw=2 sts=2:
// SPDX-FileCopyrightInfo: Copyright © DUNE Project contributors, see file LICENSE.md in module root
// SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception
#ifndef DUNE_TIMER_HH
#define DUNE_TIMER_HH

#include <chrono>

namespace Dune {

  /** @addtogroup Common
     @{
   */

  /*! \file
      \brief A simple timing class.
   */


  /** \brief A simple stop watch

     This class reports the elapsed real time, i.e. time elapsed
     after Timer::reset(). It does not measure the time spent computing,
     i.e. time spend in concurrent threads is not added up while
     time measurements include the time elapsed while sleeping.

     The class is basically a wrapper around std::chrono::high_resolution_clock::now().
   */
  class Timer
  {
    using Clock = std::chrono::high_resolution_clock;
    using Units = std::chrono::duration<double>; // seconds stored as double
  public:

    /** \brief A new timer, create and reset
     *
     * \param startImmediately If true (default) the timer starts counting immediately
     */
    Timer (bool startImmediately=true) noexcept
    {
      isRunning_ = startImmediately;
      reset();
    }

    //! Reset timer while keeping the running/stopped state
    void reset() noexcept
    {
      sumElapsed_ = std::chrono::seconds{0};
      storedLastElapsed_ = std::chrono::seconds{0};
      cstart = Clock::now();
    }


    //! Start the timer and continue measurement if it is not running. Otherwise do nothing.
    void start() noexcept
    {
      if (not (isRunning_))
      {
        cstart = Clock::now();
        isRunning_ = true;
      }
    }


    //! Get elapsed user-time from last reset until now/last stop in seconds.
    double elapsed () const noexcept
    {
      return durationCast(rawElapsed());
    }

    //! Get elapsed user-time from last start until now/last stop in seconds.
    double lastElapsed () const noexcept
    {
      return durationCast(rawLastElapsed());
    }

    //! Stop the timer and return elapsed().
    double stop() noexcept
    {
      if (isRunning_)
      {
        // update storedLastElapsed_ and  sumElapsed_ and stop timer
        storedLastElapsed_ = rawLastElapsed();
        sumElapsed_ += storedLastElapsed_;
        isRunning_ = false;
      }
      return elapsed();
    }


  private:

    bool isRunning_;
    Clock::duration sumElapsed_;
    Clock::duration storedLastElapsed_;

    Clock::duration rawElapsed () const noexcept
    {
      // if timer is running add the time elapsed since last start to sum
      if (isRunning_)
        return sumElapsed_ + rawLastElapsed();

      return sumElapsed_;
    }

    //! Get elapsed user-time from last start until now/last stop in seconds.
    Clock::duration rawLastElapsed () const noexcept
    {
      // if timer is running return the current value
      if (isRunning_)
        return Clock::now() - cstart;

      // if timer is not running return stored value from last run
      return storedLastElapsed_;
    }

    double durationCast(Clock::duration duration) const noexcept {
      return std::chrono::duration_cast<Units>(duration).count();
    }

    Clock::time_point cstart;
  }; // end class Timer

  /** @} end documentation */

} // end namespace

#endif