OpenVDB: nanovdb/examples/ex_util/CpuTimer.h Source File

OpenVDB 10.0.1

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
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
16namespace nanovdb {
17
18template <typename Accuracy = std::chrono::milliseconds>
19 class CpuTimer
20{
21 std::chrono::high_resolution_clock::time_point mStart;
22public:
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;
40 } else if (std::is_same<Accuracy, std::chrono::milliseconds>::value) {
41 os << " milliseconds" << std::endl;
42 } else if (std::is_same<Accuracy, std::chrono::seconds>::value) {
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
nanovdb::CpuTimer
Definition: CpuTimer.h:20
nanovdb::CpuTimer::CpuTimer
CpuTimer()
Definition: CpuTimer.h:23
nanovdb::CpuTimer::restart
void restart(const std::string &msg, std::ostream &os=std::cerr)
Definition: CpuTimer.h:28
nanovdb::CpuTimer::stop
void stop(std::ostream &os=std::cerr)
Definition: CpuTimer.h:33
nanovdb::CpuTimer::start
void start(const std::string &msg, std::ostream &os=std::cerr)
Definition: CpuTimer.h:24
nanovdb
Definition: NanoVDB.h:208