ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
Timer.cpp
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - CloudViewer: www.cloudViewer.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2024 www.cloudViewer.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
8 #include "Timer.h"
9 
10 #include <chrono>
11 
12 #include "Logging.h"
13 
14 namespace cloudViewer {
15 namespace utility {
16 
18  : start_time_in_milliseconds_(0.0), end_time_in_milliseconds_(0.0) {}
19 
21 
23  std::chrono::duration<double, std::milli> current_time =
24  std::chrono::high_resolution_clock::now().time_since_epoch();
25  return current_time.count();
26 }
27 
28 void Timer::Start() {
29  start_time_in_milliseconds_ = GetSystemTimeInMilliseconds();
30 }
31 
32 void Timer::Stop() {
33  end_time_in_milliseconds_ = GetSystemTimeInMilliseconds();
34 }
35 
37  return end_time_in_milliseconds_ - start_time_in_milliseconds_;
38 }
39 
41  return (end_time_in_milliseconds_ - start_time_in_milliseconds_) / 1000.0;
42 }
43 
44 std::tuple<int, int, double> Timer::GetDurationInHMS() const {
45  double duration = GetDurationInSecond();
46  int hours = static_cast<int>(duration / 3600.0);
47  int minutes = static_cast<int>((duration - hours * 3600.0) / 60.0);
48  double seconds = duration - hours * 3600.0 - minutes * 60.0;
49  return std::make_tuple(hours, minutes, seconds);
50 }
51 
52 void Timer::Print(const std::string &timer_info) const {
53  LogInfo("{} {:.2f} ms.", timer_info,
54  end_time_in_milliseconds_ - start_time_in_milliseconds_);
55 }
56 
57 ScopeTimer::ScopeTimer(const std::string &scope_timer_info /* = ""*/)
58  : scope_timer_info_(scope_timer_info) {
59  Timer::Start();
60 }
61 
63  Timer::Stop();
64  Timer::Print(scope_timer_info_ + " took");
65 }
66 
67 FPSTimer::FPSTimer(const std::string &fps_timer_info /* = ""*/,
68  int expectation /* = -1*/,
69  double time_to_print /* = 3000.0*/,
70  int events_to_print /* = 100*/)
71  : fps_timer_info_(fps_timer_info),
72  expectation_(expectation),
73  time_to_print_(time_to_print),
74  events_to_print_(events_to_print),
75  event_fragment_count_(0),
76  event_total_count_(0) {
77  Start();
78 }
79 
81  event_fragment_count_++;
82  event_total_count_++;
83  Stop();
84  if (GetDurationInMillisecond() >= time_to_print_ ||
85  event_fragment_count_ >= events_to_print_) {
86  // print and reset
87  if (expectation_ == -1) {
88  LogInfo("{} at {:.2f} fps.", fps_timer_info_,
89  double(event_fragment_count_ + 1) /
90  GetDurationInMillisecond() * 1000.0);
91  } else {
92  LogInfo("{} at {:.2f} fps (progress {:.2f}%).",
93  fps_timer_info_.c_str(),
94  static_cast<double>(event_fragment_count_ + 1) /
95  GetDurationInMillisecond() * 1000.0,
96  static_cast<double>(event_total_count_ * 100.0) /
97  static_cast<double>(expectation_));
98  }
99  Start();
100  event_fragment_count_ = 0;
101  }
102 }
103 
104 } // namespace utility
105 } // namespace cloudViewer
FPSTimer(const std::string &fps_timer_info="", int expectation=-1, double time_to_print=3000.0, int events_to_print=100)
Definition: Timer.cpp:67
ScopeTimer(const std::string &scope_timer_info="")
Definition: Timer.cpp:57
double GetDurationInMillisecond() const
Definition: Timer.cpp:36
void Print(const std::string &timer_info) const
Definition: Timer.cpp:52
double GetDurationInSecond() const
Definition: Timer.cpp:40
std::tuple< int, int, double > GetDurationInHMS() const
Definition: Timer.cpp:44
static double GetSystemTimeInMilliseconds()
Definition: Timer.cpp:22
#define LogInfo(...)
Definition: Logging.h:81
Generic file read and write utility for python interface.
#define seconds
Definition: rename.h:375