OpenVDB 11.0.0

Loading...
Searching...
No Matches
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;
58 } else if (std::is_same<AccuracyT, std::chrono::milliseconds>::value) {
59 os << " milliseconds" << std::endl;
60 } else if (std::is_same<AccuracyT, std::chrono::seconds>::value) {
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
nanovdb::CpuTimer::CpuTimer
CpuTimer()
Default constructor.
Definition CpuTimer.h:23
nanovdb::CpuTimer::restart
void restart(const std::string &msg, std::ostream &os=std::cerr)
stop and start the timer
Definition CpuTimer.h:72
nanovdb::CpuTimer::CpuTimer
CpuTimer(const std::string &msg, std::ostream &os=std::cerr)
Constructor that starts the timer.
Definition CpuTimer.h:28
nanovdb::CpuTimer::stop
void stop(std::ostream &os=std::cerr)
stop the timer
Definition CpuTimer.h:51
nanovdb::CpuTimer::elapsed
auto elapsed()
elapsed time (since start) in miliseconds
Definition CpuTimer.h:41
nanovdb::CpuTimer::start
void start(const std::string &msg, std::ostream &os=std::cerr)
Start the timer.
Definition CpuTimer.h:33
nanovdb
Definition NanoVDB.h:247