HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
CpuTimer.h
Go to the documentation of this file.
1 // Copyright Contributors to the OpenVDB Project
2 // SPDX-License-Identifier: MPL-2.0
3 
4 /// @file CpuTimer.h
5 ///
6 /// @author Ken Museth
7 ///
8 /// @brief A simple timing class (in case openvdb::util::CpuTimer is unavailable)
9 
10 #ifndef NANOVDB_CPU_TIMER_H_HAS_BEEN_INCLUDED
11 #define NANOVDB_CPU_TIMER_H_HAS_BEEN_INCLUDED
12 
13 #include <iostream>
14 #include <chrono>
15 
16 namespace nanovdb {
17 
18 class CpuTimer
19 {
20  std::chrono::high_resolution_clock::time_point mStart;
21 public:
22  /// @brief Default constructor
23  CpuTimer() {}
24 
25  /// @brief Constructor that starts the timer
26  /// @param msg string message to be printed when timer is started
27  /// @param os output stream for the message above
28  CpuTimer(const std::string &msg, std::ostream& os = std::cerr) {this->start(msg, os);}
29 
30  /// @brief Start the timer
31  /// @param msg string message to be printed when timer is started
32  /// @param os output stream for the message above
33  void start(const std::string &msg, std::ostream& os = std::cerr)
34  {
35  os << msg << " ... " << std::flush;
36  mStart = std::chrono::high_resolution_clock::now();
37  }
38 
39  /// @brief elapsed time (since start) in miliseconds
40  template <typename AccuracyT = std::chrono::milliseconds>
41  auto elapsed()
42  {
43  auto end = std::chrono::high_resolution_clock::now();
44  return std::chrono::duration_cast<AccuracyT>(end - mStart).count();
45  }
46 
47  /// @brief stop the timer
48  /// @tparam AccuracyT Template parameter defining the accuracy of the reported times
49  /// @param os output stream for the message above
50  template <typename AccuracyT = std::chrono::milliseconds>
51  void stop(std::ostream& os = std::cerr)
52  {
53  auto end = std::chrono::high_resolution_clock::now();
54  auto diff = std::chrono::duration_cast<AccuracyT>(end - mStart).count();
55  os << "completed in " << diff;
56  if (std::is_same<AccuracyT, std::chrono::microseconds>::value) {// resolved at compile-time
57  os << " microseconds" << std::endl;
59  os << " milliseconds" << std::endl;
61  os << " seconds" << std::endl;
62  } else {
63  os << " unknown time unit" << std::endl;
64  }
65  }
66 
67  /// @brief stop and start the timer
68  /// @tparam AccuracyT Template parameter defining the accuracy of the reported times
69  /// @param msg string message to be printed when timer is started
70  /// @param os output stream for the message above
71  template <typename AccuracyT = std::chrono::milliseconds>
72  void restart(const std::string &msg, std::ostream& os = std::cerr)
73  {
74  this->stop<AccuracyT>();
75  this->start(msg, os);
76  }
77 
78 
79 };// CpuTimer
80 
81 } // namespace nanovdb
82 
83 #endif // NANOVDB_CPU_TIMER_HAS_BEEN_INCLUDED
void start(const std::string &msg, std::ostream &os=std::cerr)
Start the timer.
Definition: CpuTimer.h:33
void restart(const std::string &msg, std::ostream &os=std::cerr)
stop and start the timer
Definition: CpuTimer.h:72
GLsizei const GLchar *const * string
Definition: glcorearb.h:814
GLsizei const GLfloat * value
Definition: glcorearb.h:824
auto elapsed()
elapsed time (since start) in miliseconds
Definition: CpuTimer.h:41
CpuTimer(const std::string &msg, std::ostream &os=std::cerr)
Constructor that starts the timer.
Definition: CpuTimer.h:28
void stop(std::ostream &os=std::cerr)
stop the timer
Definition: CpuTimer.h:51
GLuint GLuint end
Definition: glcorearb.h:475
CpuTimer()
Default constructor.
Definition: CpuTimer.h:23
GLint GLsizei count
Definition: glcorearb.h:405