OpenVDB 12.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
16namespace nanovdb {
17
18namespace util {
19
20class Timer
21{
22 std::chrono::high_resolution_clock::time_point mStart;
23public:
24 /// @brief Default constructor
25 Timer() {}
26
27 /// @brief Constructor that starts the timer
28 /// @param msg string message to be printed when timer is started
29 /// @param os output stream for the message above
30 Timer(const std::string &msg, std::ostream& os = std::cerr) {this->start(msg, os);}
31
32 /// @brief Start the timer
33 /// @param msg string message to be printed when timer is started
34 /// @param os output stream for the message above
35 void start(const std::string &msg, std::ostream& os = std::cerr)
36 {
37 os << msg << " ... " << std::flush;
38 mStart = std::chrono::high_resolution_clock::now();
39 }
40
41 /// @brief elapsed time (since start) in miliseconds
42 template <typename AccuracyT = std::chrono::milliseconds>
43 auto elapsed()
44 {
45 auto end = std::chrono::high_resolution_clock::now();
46 return std::chrono::duration_cast<AccuracyT>(end - mStart).count();
47 }
48
49 /// @brief stop the timer
50 /// @tparam AccuracyT Template parameter defining the accuracy of the reported times
51 /// @param os output stream for the message above
52 template <typename AccuracyT = std::chrono::milliseconds>
53 void stop(std::ostream& os = std::cerr)
54 {
55 auto end = std::chrono::high_resolution_clock::now();
56 auto diff = std::chrono::duration_cast<AccuracyT>(end - mStart).count();
57 os << "completed in " << diff;
58 if (std::is_same<AccuracyT, std::chrono::microseconds>::value) {// resolved at compile-time
59 os << " microseconds" << std::endl;
60 } else if (std::is_same<AccuracyT, std::chrono::milliseconds>::value) {
61 os << " milliseconds" << std::endl;
62 } else if (std::is_same<AccuracyT, std::chrono::seconds>::value) {
63 os << " seconds" << std::endl;
64 } else {
65 os << " unknown time unit" << std::endl;
66 }
67 }
68
69 /// @brief stop and start the timer
70 /// @tparam AccuracyT Template parameter defining the accuracy of the reported times
71 /// @param msg string message to be printed when timer is started
72 /// @param os output stream for the message above
73 template <typename AccuracyT = std::chrono::milliseconds>
74 void restart(const std::string &msg, std::ostream& os = std::cerr)
75 {
76 this->stop<AccuracyT>();
77 this->start(msg, os);
78 }
79};// Timer
80
81}// namespace util
82
83using CpuTimer [[deprecated("Use nanovdb::util::Timer instead")]] = util::Timer;
84
85} // namespace nanovdb
86
87#endif // NANOVDB_UTIL_TIMER_HAS_BEEN_INCLUDED
Timer()
Default constructor.
Definition Timer.h:25
Timer()
Default constructor.
Definition Timer.h:25
void restart(const std::string &msg, std::ostream &os=std::cerr)
stop and start the timer
Definition Timer.h:74
void stop(std::ostream &os=std::cerr)
stop the timer
Definition Timer.h:53
auto elapsed()
elapsed time (since start) in miliseconds
Definition Timer.h:43
void start(const std::string &msg, std::ostream &os=std::cerr)
Start the timer.
Definition Timer.h:35
Timer(const std::string &msg, std::ostream &os=std::cerr)
Constructor that starts the timer.
Definition Timer.h:30
Definition ForEach.h:29
Definition GridHandle.h:27