OpenVDB 14.0.0
Loading...
Searching...
No Matches
Timer.h
Go to the documentation of this file.
1// Copyright Contributors to the OpenVDB Project
2// SPDX-License-Identifier: Apache-2.0
3
4/// @file nanovdb/util/Timer.h
5///
6/// @author Ken Museth
7///
8/// @brief A simple timing class (in case openvdb::util::CpuTimer is unavailable)
9
10#ifndef NANOVDB_UTIL_TIMER_H_HAS_BEEN_INCLUDED
11#define NANOVDB_UTIL_TIMER_H_HAS_BEEN_INCLUDED
12
13#include <iostream>
14#include <chrono>
15#include <string>
16#include <type_traits> // for std::is_same
17
18namespace nanovdb {
19
20namespace util {
21
22class Timer
23{
24 std::chrono::high_resolution_clock::time_point mStart, mStop;
25public:
26 /// @brief Default constructor
27 Timer() {}
28
29 /// @brief Constructor that starts the timer
30 /// @param msg string message to be printed when timer is started
31 /// @param os output stream for the message above
32 Timer(const std::string &msg, std::ostream& os = std::cerr) {this->start(msg, os);}
33
34 /// @brief Start the timer
35 /// @param msg string message to be printed when timer is started
36 /// @param os output stream for the message above
37 void start(const std::string &msg, std::ostream& os = std::cerr)
38 {
39 os << msg << " ... " << std::flush;
40 mStart = std::chrono::high_resolution_clock::now();
41 }
42
43 /// @brief Record the stop time so the elapsed time since start can be computed
44 void record()
45 {
46 mStop = std::chrono::high_resolution_clock::now();
47 }
48
49 /// @brief Returns the time in milliseconds since record was called
50 float milliseconds() const
51 {
52 return static_cast<float>(std::chrono::duration_cast<std::chrono::milliseconds>(mStop - mStart).count());
53 }
54
55 /// @brief call record and return the elapsed time (since start) in miliseconds
56 template <typename AccuracyT = std::chrono::milliseconds>
57 auto elapsed()
58 {
59 this->record();
60 return std::chrono::duration_cast<AccuracyT>(mStop - mStart).count();
61 }
62
63 /// @brief stop the timer and print elapsed time to a stream
64 /// @tparam AccuracyT Template parameter defining the accuracy of the reported times
65 /// @param os output stream for the message above
66 template <typename AccuracyT = std::chrono::milliseconds>
67 void stop(std::ostream& os = std::cerr)
68 {
69 mStop = std::chrono::high_resolution_clock::now();
70 auto diff = std::chrono::duration_cast<AccuracyT>(mStop - mStart).count();
71 os << "completed in " << diff;
72 if (std::is_same<AccuracyT, std::chrono::microseconds>::value) {// resolved at compile-time
73 os << " microseconds" << std::endl;
74 } else if (std::is_same<AccuracyT, std::chrono::milliseconds>::value) {
75 os << " milliseconds" << std::endl;
76 } else if (std::is_same<AccuracyT, std::chrono::seconds>::value) {
77 os << " seconds" << std::endl;
78 } else {
79 os << " unknown time unit" << std::endl;
80 }
81 }
82
83 /// @brief stop and start the timer again
84 /// @tparam AccuracyT Template parameter defining the accuracy of the reported times
85 /// @param msg string message to be printed when timer is started
86 /// @param os output stream for the message above
87 template <typename AccuracyT = std::chrono::milliseconds>
88 void restart(const std::string &msg, std::ostream& os = std::cerr)
89 {
90 this->stop<AccuracyT>();
91 this->start(msg, os);
92 }
93};// Timer
94
95}// namespace util
96
97} // namespace nanovdb
98
99#endif // NANOVDB_UTIL_TIMER_HAS_BEEN_INCLUDED
Timer()
Default constructor.
Definition Timer.h:27
void restart(const std::string &msg, std::ostream &os=std::cerr)
stop and start the timer again
Definition Timer.h:88
void stop(std::ostream &os=std::cerr)
stop the timer and print elapsed time to a stream
Definition Timer.h:67
auto elapsed()
call record and return the elapsed time (since start) in miliseconds
Definition Timer.h:57
void start(const std::string &msg, std::ostream &os=std::cerr)
Start the timer.
Definition Timer.h:37
void record()
Record the stop time so the elapsed time since start can be computed.
Definition Timer.h:44
float milliseconds() const
Returns the time in milliseconds since record was called.
Definition Timer.h:50
Timer(const std::string &msg, std::ostream &os=std::cerr)
Constructor that starts the timer.
Definition Timer.h:32
Definition GridChecksum.h:86
Defines a simple memory pool used to call cub functions that use dynamic temporary storage.
Definition GridHandle.h:34