OpenVDB
13.0.0
Toggle main menu visibility
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
16
namespace
nanovdb
{
17
18
namespace
util
{
19
20
class
Timer
21
{
22
std::chrono::high_resolution_clock::time_point mStart, mStop;
23
public
:
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 Record the stop time so the elapsed time since start can be computed
42
void
record
()
43
{
44
mStop = std::chrono::high_resolution_clock::now();
45
}
46
47
/// @brief Returns the time in milliseconds since record was called
48
float
milliseconds
()
const
49
{
50
return
std::chrono::duration_cast<std::chrono::milliseconds>(mStop - mStart).count();
51
}
52
53
/// @brief call record and return the elapsed time (since start) in miliseconds
54
template
<
typename
AccuracyT = std::chrono::milliseconds>
55
auto
elapsed
()
56
{
57
this->
record
();
58
return
std::chrono::duration_cast<AccuracyT>(mStop - mStart).count();
59
}
60
61
/// @brief stop the timer and print elapsed time to a stream
62
/// @tparam AccuracyT Template parameter defining the accuracy of the reported times
63
/// @param os output stream for the message above
64
template
<
typename
AccuracyT = std::chrono::milliseconds>
65
void
stop
(std::ostream& os = std::cerr)
66
{
67
mStop = std::chrono::high_resolution_clock::now();
68
auto
diff = std::chrono::duration_cast<AccuracyT>(mStop - mStart).count();
69
os <<
"completed in "
<< diff;
70
if
(std::is_same<AccuracyT, std::chrono::microseconds>::value) {
// resolved at compile-time
71
os <<
" microseconds"
<< std::endl;
72
}
else
if
(std::is_same<AccuracyT, std::chrono::milliseconds>::value) {
73
os <<
" milliseconds"
<< std::endl;
74
}
else
if
(std::is_same<AccuracyT, std::chrono::seconds>::value) {
75
os <<
" seconds"
<< std::endl;
76
}
else
{
77
os <<
" unknown time unit"
<< std::endl;
78
}
79
}
80
81
/// @brief stop and start the timer again
82
/// @tparam AccuracyT Template parameter defining the accuracy of the reported times
83
/// @param msg string message to be printed when timer is started
84
/// @param os output stream for the message above
85
template
<
typename
AccuracyT = std::chrono::milliseconds>
86
void
restart
(
const
std::string &msg, std::ostream& os = std::cerr)
87
{
88
this->
stop<AccuracyT>
();
89
this->
start
(msg, os);
90
}
91
};
// Timer
92
93
}
// namespace util
94
95
using
CpuTimer
[[deprecated(
"Use nanovdb::util::Timer instead"
)]] =
util::Timer
;
96
97
}
// namespace nanovdb
98
99
#endif
// NANOVDB_UTIL_TIMER_HAS_BEEN_INCLUDED
nanovdb::util::Timer
Definition
Timer.h:21
nanovdb::util::Timer::Timer
Timer()
Default constructor.
Definition
Timer.h:25
nanovdb::util::Timer::restart
void restart(const std::string &msg, std::ostream &os=std::cerr)
stop and start the timer again
Definition
Timer.h:86
nanovdb::util::Timer::stop
void stop(std::ostream &os=std::cerr)
stop the timer and print elapsed time to a stream
Definition
Timer.h:65
nanovdb::util::Timer::elapsed
auto elapsed()
call record and return the elapsed time (since start) in miliseconds
Definition
Timer.h:55
nanovdb::util::Timer::start
void start(const std::string &msg, std::ostream &os=std::cerr)
Start the timer.
Definition
Timer.h:35
nanovdb::util::Timer::record
void record()
Record the stop time so the elapsed time since start can be computed.
Definition
Timer.h:42
nanovdb::util::Timer::milliseconds
float milliseconds() const
Returns the time in milliseconds since record was called.
Definition
Timer.h:48
nanovdb::util::Timer::Timer
Timer(const std::string &msg, std::ostream &os=std::cerr)
Constructor that starts the timer.
Definition
Timer.h:30
nanovdb::util
Definition
ForEach.h:29
nanovdb
Definition
GridHandle.h:27
nanovdb::CpuTimer
util::Timer CpuTimer
nanovdb
util
Timer.h
Generated by
1.18.0