Line data Source code
1 : /****************************************************************************/
2 : // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3 : // Copyright (C) 2020-2024 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 : /****************************************************************************/
14 : /// @file StopWatch.h
15 : /// @author Michael Behrisch
16 : /// @date 2020-09-09
17 : ///
18 : // A stop watch for keeping time,
19 : // based on https://github.com/vukis/Cpp-Utilities/tree/master/ThreadPool
20 : /****************************************************************************/
21 : #pragma once
22 : #include <config.h>
23 :
24 : #include <vector>
25 : #include <chrono>
26 : #include <numeric>
27 :
28 : template < typename TimeT = std::chrono::milliseconds, typename ClockT =
29 : #if defined(_MSC_VER) && _MSC_VER == 1800
30 : std::chrono::system_clock
31 : #else
32 : std::chrono::steady_clock
33 : #endif
34 : >
35 6094836 : class StopWatch {
36 : public:
37 6607890 : StopWatch(const bool calibrate = false) : myTimingCost(0) {
38 6607890 : if (calibrate) {
39 0 : myStart = ClockT::now();
40 0 : for (int i = 0; i < 1000; i++) {
41 0 : myEnd = ClockT::now();
42 : }
43 0 : myTimingCost = std::chrono::duration_cast<TimeT>(myEnd - myStart) / 1000;
44 : }
45 : start();
46 6607890 : }
47 :
48 : void start() {
49 6607890 : myStart = myEnd = ClockT::now();
50 : }
51 :
52 : long long int stop() {
53 : myEnd = ClockT::now();
54 : return elapsed();
55 : }
56 :
57 : long long int elapsed() const {
58 : const TimeT& delta = std::chrono::duration_cast<TimeT>(myEnd - myStart) - (2 * myTimingCost);
59 : myHistory.push_back(delta);
60 : return (long long int)delta.count();
61 : }
62 :
63 : void add(const StopWatch<TimeT, ClockT>& other) {
64 : myHistory.insert(myHistory.end(), other.myHistory.begin(), other.myHistory.end());
65 : }
66 :
67 : const std::vector<TimeT>& getHistory() const {
68 : return myHistory;
69 : }
70 :
71 : long long int getAverage() const {
72 : return (long long int)(std::accumulate(myHistory.begin(), myHistory.end(), TimeT{}) / myHistory.size()).count();
73 : }
74 :
75 : long long int getTotal() const {
76 : return (long long int)(std::accumulate(myHistory.begin(), myHistory.end(), TimeT{})).count();
77 : }
78 :
79 : private:
80 : std::chrono::time_point<ClockT> myStart;
81 : std::chrono::time_point<ClockT> myEnd;
82 : TimeT myTimingCost;
83 : mutable std::vector<TimeT> myHistory;
84 : };
|