ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
timer.cc
Go to the documentation of this file.
1 // Copyright (c) 2018, ETH Zurich and UNC Chapel Hill.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are met:
6 //
7 // * Redistributions of source code must retain the above copyright
8 // notice, this list of conditions and the following disclaimer.
9 //
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 //
14 // * Neither the name of ETH Zurich and UNC Chapel Hill nor the names of
15 // its contributors may be used to endorse or promote products derived
16 // from this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
22 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 // POSSIBILITY OF SUCH DAMAGE.
29 //
30 // Author: Johannes L. Schoenberger (jsch-at-demuc-dot-de)
31 
32 #include "util/timer.h"
33 
34 #include "util/logging.h"
35 #include "util/misc.h"
36 
37 using namespace std::chrono;
38 
39 namespace colmap {
40 
41 Timer::Timer() : started_(false), paused_(false) {}
42 
43 void Timer::Start() {
44  started_ = true;
45  paused_ = false;
46  start_time_ = high_resolution_clock::now();
47 }
48 
50  started_ = false;
51  Start();
52 }
53 
54 void Timer::Pause() {
55  paused_ = true;
56  pause_time_ = high_resolution_clock::now();
57 }
58 
59 void Timer::Resume() {
60  paused_ = false;
61  start_time_ += high_resolution_clock::now() - pause_time_;
62 }
63 
64 void Timer::Reset() {
65  started_ = false;
66  paused_ = false;
67 }
68 
70  if (!started_) {
71  return 0.0;
72  }
73  if (paused_) {
74  return duration_cast<microseconds>(pause_time_ - start_time_).count();
75  } else {
76  return duration_cast<microseconds>(high_resolution_clock::now() -
77  start_time_)
78  .count();
79  }
80 }
81 
82 double Timer::ElapsedSeconds() const { return ElapsedMicroSeconds() / 1e6; }
83 
84 double Timer::ElapsedMinutes() const { return ElapsedSeconds() / 60; }
85 
86 double Timer::ElapsedHours() const { return ElapsedMinutes() / 60; }
87 
88 void Timer::PrintSeconds() const {
89  std::cout << StringPrintf("Elapsed time: %.5f [seconds]", ElapsedSeconds())
90  << std::endl;
91 }
92 
93 void Timer::PrintMinutes() const {
94  std::cout << StringPrintf("Elapsed time: %.3f [minutes]", ElapsedMinutes())
95  << std::endl;
96 }
97 
98 void Timer::PrintHours() const {
99  std::cout << StringPrintf("Elapsed time: %.3f [hours]", ElapsedHours())
100  << std::endl;
101 }
102 
103 } // namespace colmap
void Start()
Definition: timer.cc:43
void Pause()
Definition: timer.cc:54
double ElapsedHours() const
Definition: timer.cc:86
void Restart()
Definition: timer.cc:49
double ElapsedSeconds() const
Definition: timer.cc:82
void Reset()
Definition: timer.cc:64
double ElapsedMinutes() const
Definition: timer.cc:84
double ElapsedMicroSeconds() const
Definition: timer.cc:69
void PrintSeconds() const
Definition: timer.cc:88
void PrintHours() const
Definition: timer.cc:98
void Resume()
Definition: timer.cc:59
void PrintMinutes() const
Definition: timer.cc:93
QTextStream & endl(QTextStream &stream)
Definition: QtCompat.h:718
std::string StringPrintf(const char *format,...)
Definition: string.cc:131