Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
StopWatch.h
Go to the documentation of this file.
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/****************************************************************************/
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
28template < 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 >
35class StopWatch {
36public:
37 StopWatch(const bool calibrate = false) : myTimingCost(0) {
38 if (calibrate) {
39 myStart = ClockT::now();
40 for (int i = 0; i < 1000; i++) {
41 myEnd = ClockT::now();
42 }
43 myTimingCost = std::chrono::duration_cast<TimeT>(myEnd - myStart) / 1000;
44 }
45 start();
46 }
47
48 void start() {
49 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
79private:
80 std::chrono::time_point<ClockT> myStart;
81 std::chrono::time_point<ClockT> myEnd;
83 mutable std::vector<TimeT> myHistory;
84};
TimeT myTimingCost
Definition StopWatch.h:82
StopWatch(const bool calibrate=false)
Definition StopWatch.h:37
long long int getTotal() const
Definition StopWatch.h:75
long long int getAverage() const
Definition StopWatch.h:71
std::chrono::time_point< ClockT > myStart
Definition StopWatch.h:80
std::vector< TimeT > myHistory
Definition StopWatch.h:83
long long int elapsed() const
Definition StopWatch.h:57
void add(const StopWatch< TimeT, ClockT > &other)
Definition StopWatch.h:63
long long int stop()
Definition StopWatch.h:52
std::chrono::time_point< ClockT > myEnd
Definition StopWatch.h:81
void start()
Definition StopWatch.h:48
const std::vector< TimeT > & getHistory() const
Definition StopWatch.h:67