OpenVDB  9.0.1
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
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 template <typename Accuracy = std::chrono::milliseconds>
19 class CpuTimer
20 {
21  std::chrono::high_resolution_clock::time_point mStart;
22 public:
23  CpuTimer() {}
24  void start(const std::string &msg, std::ostream& os = std::cerr) {
25  os << msg << " ... " << std::flush;
26  mStart = std::chrono::high_resolution_clock::now();
27  }
28  void restart(const std::string &msg, std::ostream& os = std::cerr) {
29  this->stop();
30  os << msg << " ... " << std::flush;
31  mStart = std::chrono::high_resolution_clock::now();
32  }
33  void stop(std::ostream& os = std::cerr)
34  {
35  auto end = std::chrono::high_resolution_clock::now();
36  auto diff = std::chrono::duration_cast<Accuracy>(end - mStart).count();
37  os << "completed in " << diff;
38  if (std::is_same<Accuracy, std::chrono::microseconds>::value) {// resolved at compile-time
39  os << " microseconds" << std::endl;
41  os << " milliseconds" << std::endl;
43  os << " seconds" << std::endl;
44  } else {
45  os << " unknown time unit" << std::endl;
46  }
47  }
48 };// CpuTimer
49 
50 } // namespace nanovdb
51 
52 #endif // NANOVDB_CPU_TIMER_HAS_BEEN_INCLUDED
void start(const std::string &msg, std::ostream &os=std::cerr)
Definition: CpuTimer.h:24
Definition: NanoVDB.h:184
CpuTimer()
Definition: CpuTimer.h:23
ValueT value
Definition: GridBuilder.h:1287
Definition: CpuTimer.h:19
void restart(const std::string &msg, std::ostream &os=std::cerr)
Definition: CpuTimer.h:28
void stop(std::ostream &os=std::cerr)
Definition: CpuTimer.h:33