DART 6.13.2
Loading...
Searching...
No Matches
Stopwatch.hpp
Go to the documentation of this file.
1/*
2 * Copyright (c) 2011-2022, The DART development contributors
3 * All rights reserved.
4 *
5 * The list of contributors can be found at:
6 * https://github.com/dartsim/dart/blob/master/LICENSE
7 *
8 * This file is provided under the following "BSD-style" License:
9 * Redistribution and use in source and binary forms, with or
10 * without modification, are permitted provided that the following
11 * conditions are met:
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * * Redistributions in binary form must reproduce the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer in the documentation and/or other materials provided
17 * with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
26 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33#ifndef DART_COMMON_STOPWATCH_HPP_
34#define DART_COMMON_STOPWATCH_HPP_
35
36#include <chrono>
37#include <iostream>
38
39namespace dart::common {
40
42template <
43 typename UnitType,
44 typename ClockType = std::chrono::high_resolution_clock>
45class Stopwatch final
46{
47public:
52 explicit Stopwatch(bool start = true);
53
55 ~Stopwatch();
56
58 [[nodiscard]] bool isStarted() const;
59
61 void start();
62
64 void stop();
65
70 void reset();
71
73 [[nodiscard]] double elapsedS() const;
74
76 [[nodiscard]] double elapsedMS() const;
77
79 [[nodiscard]] double elapsedUS() const;
80
82 [[nodiscard]] double elapsedNS() const;
83
85 void print(std::ostream& os = std::cout) const;
86
88 template <typename T, typename U>
89 friend std::ostream& operator<<(std::ostream& os, const Stopwatch<T, U>& sw);
90
91private:
92 // Deletes copy/move constructors and assign/move operators
93 Stopwatch(const Stopwatch&) = delete;
94 Stopwatch(Stopwatch&&) = delete;
95 Stopwatch& operator=(const Stopwatch&) = delete;
97
99 [[nodiscard]] UnitType duration() const;
100
102 typename ClockType::time_point mStart;
103
105 UnitType mElapsed;
106
109};
110
117void tic();
118
120double toc(bool print = false);
121
123double tocS(bool print = false);
124
126double tocMS(bool print = false);
127
129double tocUS(bool print = false);
130
132double tocNS(bool print = false);
133
138
139} // namespace dart::common
140
142
143#endif // DART_COMMON_STOPWATCH_HPP_
Simple stopwatch implementation.
Definition Stopwatch.hpp:46
bool mPaused
Whether the stopwatch is paused.
Definition Stopwatch.hpp:108
Stopwatch & operator=(Stopwatch &&)=delete
double elapsedMS() const
Returns the elapsed time in milliseconds.
Definition Stopwatch-impl.hpp:140
double elapsedS() const
Returns the elapsed time in seconds.
Definition Stopwatch-impl.hpp:118
void stop()
Stops the stopwatch.
Definition Stopwatch-impl.hpp:97
void start()
Starts the stopwatch.
Definition Stopwatch-impl.hpp:84
void reset()
Resets the stopwatch.
Definition Stopwatch-impl.hpp:110
bool isStarted() const
Returns whether the stopwatch is started.
Definition Stopwatch-impl.hpp:77
ClockType::time_point mStart
The start time.
Definition Stopwatch.hpp:102
void print(std::ostream &os=std::cout) const
Prints state of the stopwatch.
Definition Stopwatch-impl.hpp:222
Stopwatch(const Stopwatch &)=delete
UnitType mElapsed
The elapsed time.
Definition Stopwatch.hpp:105
double elapsedNS() const
Returns the elapsed time in nanoseconds.
Definition Stopwatch-impl.hpp:186
~Stopwatch()
Destructor.
Definition Stopwatch-impl.hpp:70
friend std::ostream & operator<<(std::ostream &os, const Stopwatch< T, U > &sw)
Prints state of the stopwatch.
Stopwatch(Stopwatch &&)=delete
double elapsedUS() const
Returns the elapsed time in microseconds.
Definition Stopwatch-impl.hpp:163
UnitType duration() const
Returns the duration.
Definition Stopwatch-impl.hpp:208
Stopwatch & operator=(const Stopwatch &)=delete
Definition Aspect.cpp:42
double tocUS(bool print)
Returns the elapsed time in microseconds since the last tic() call.
Definition Stopwatch.cpp:84
double tocS(bool print)
Returns the elapsed time in seconds since the last tic() call.
Definition Stopwatch.cpp:54
double tocMS(bool print)
Returns the elapsed time in milliseconds since the last tic() call.
Definition Stopwatch.cpp:69
double tocNS(bool print)
Returns the elapsed time in nanoseconds since the last tic() call.
Definition Stopwatch.cpp:99
double toc(bool print)
Returns the elapsed time in seconds since the last tic() call.
Definition Stopwatch.cpp:48
void tic()
MATLAB like timer.
Definition Stopwatch.cpp:42