Eclipse SUMO - Simulation of Urban MObility
StopWatch.h
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2020-2020 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials are made available under the
5 // terms of the Eclipse Public License 2.0 which is available at
6 // https://www.eclipse.org/legal/epl-2.0/
7 // This Source Code may also be made available under the following Secondary
8 // Licenses when the conditions for such availability set forth in the Eclipse
9 // Public License 2.0 are satisfied: GNU General Public License, version 2
10 // or later which is available at
11 // https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12 // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13 /****************************************************************************/
18 // A stop watch for keeping time,
19 // based on https://github.com/vukis/Cpp-Utilities/tree/master/ThreadPool
20 /****************************************************************************/
21 #pragma once
22 
23 #include <vector>
24 #include <chrono>
25 #include <numeric>
26 
27 template < typename TimeT = std::chrono::milliseconds, typename ClockT =
28 #if defined(_MSC_VER) && _MSC_VER == 1800
29  std::chrono::system_clock
30 #else
31  std::chrono::steady_clock
32 #endif
33  >
34 class StopWatch {
35 public:
36  StopWatch(const bool calibrate = false) : myTimingCost(0) {
37  if (calibrate) {
38  myStart = ClockT::now();
39  for (int i = 0; i < 1000; i++) {
40  myEnd = ClockT::now();
41  }
42  myTimingCost = std::chrono::duration_cast<TimeT>(myEnd - myStart) / 1000;
43  }
44  start();
45  }
46 
47  void start() {
48  myStart = myEnd = ClockT::now();
49  }
50 
51  long long int stop() {
52  myEnd = ClockT::now();
53  return elapsed();
54  }
55 
56  long long int elapsed() const {
57  const TimeT& delta = std::chrono::duration_cast<TimeT>(myEnd - myStart) - (2 * myTimingCost);
58  myHistory.push_back(delta);
59  return (long long int)delta.count();
60  }
61 
62  void add(const StopWatch<TimeT, ClockT>& other) {
63  myHistory.insert(myHistory.end(), other.myHistory.begin(), other.myHistory.end());
64  }
65 
66  const std::vector<TimeT>& getHistory() const {
67  return myHistory;
68  }
69 
70  long long int getAverage() const {
71  return (long long int)(std::accumulate(myHistory.begin(), myHistory.end(), TimeT{}) / myHistory.size()).count();
72  }
73 
74  long long int getTotal() const {
75  return (long long int)(std::accumulate(myHistory.begin(), myHistory.end(), TimeT{})).count();
76  }
77 
78 private:
79  std::chrono::time_point<ClockT> myStart;
80  std::chrono::time_point<ClockT> myEnd;
81  TimeT myTimingCost;
82  mutable std::vector<TimeT> myHistory;
83 };
TimeT myTimingCost
Definition: StopWatch.h:81
StopWatch(const bool calibrate=false)
Definition: StopWatch.h:36
const std::vector< TimeT > & getHistory() const
Definition: StopWatch.h:66
long long int getTotal() const
Definition: StopWatch.h:74
long long int getAverage() const
Definition: StopWatch.h:70
std::chrono::time_point< ClockT > myStart
Definition: StopWatch.h:79
std::vector< TimeT > myHistory
Definition: StopWatch.h:82
long long int elapsed() const
Definition: StopWatch.h:56
void add(const StopWatch< TimeT, ClockT > &other)
Definition: StopWatch.h:62
long long int stop()
Definition: StopWatch.h:51
std::chrono::time_point< ClockT > myEnd
Definition: StopWatch.h:80
void start()
Definition: StopWatch.h:47