Visual Computing Library  devel
Loading...
Searching...
No Matches
timer.h
1/*****************************************************************************
2 * VCLib *
3 * Visual Computing Library *
4 * *
5 * Copyright(C) 2021-2025 *
6 * Visual Computing Lab *
7 * ISTI - Italian National Research Council *
8 * *
9 * All rights reserved. *
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the Mozilla Public License Version 2.0 as published *
13 * by the Mozilla Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 * This program is distributed in the hope that it will be useful, *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19 * Mozilla Public License Version 2.0 *
20 * (https://www.mozilla.org/en-US/MPL/2.0/) for more details. *
21 ****************************************************************************/
22
23#ifndef VCL_BASE_TIMER_H
24#define VCL_BASE_TIMER_H
25
26#include <chrono>
27#include <iostream>
28#include <string>
29
30namespace vcl {
31
41class Timer
42{
43 std::string mCaption = "Timer";
44 bool mIsStopped = false;
45
46 std::chrono::high_resolution_clock::time_point mBegin, mEnd;
47
48public:
56 Timer(bool _start = true)
57 {
58 if (_start)
59 start();
60 }
61
70 Timer(const char* caption, bool _start = true) : mCaption(caption)
71 {
72 if (_start)
73 start();
74 }
75
84 Timer(const std::string& caption, bool _start = true) : mCaption(caption)
85 {
86 if (_start)
87 start();
88 }
89
93 void start()
94 {
95 mBegin = std::chrono::high_resolution_clock::now();
96 mIsStopped = false;
97 }
98
104 {
105 stop();
106 print();
107 }
108
112 void stop()
113 {
114 mEnd = std::chrono::high_resolution_clock::now();
115 mIsStopped = true;
116 }
117
123 void print() const
124 {
125 double secs = delay();
126 int mins = (int) secs / 60;
127 if (mins == 0)
128 std::cout << "[" << secs << " secs]\t" << mCaption << std::endl;
129 else {
130 secs = secs - mins * 60;
131 int hours = mins / 60;
132 if (hours == 0)
133 std::cout << "[" << mins << " mins; " << secs << " secs]\t"
134 << mCaption << std::endl;
135 else {
136 mins = mins - hours * 60;
137 std::cout << "[" << hours << " hours; " << mins << " mins; "
138 << secs << " secs]\t" << mCaption << std::endl;
139 }
140 }
141 }
142
148 double delay() const
149 {
150 double secs;
151 if (mIsStopped) {
152 secs =
153 (double) (std::chrono::duration_cast<std::chrono::microseconds>(
154 mEnd - mBegin)
155 .count()) /
156 1000000;
157 }
158 else {
159 std::chrono::high_resolution_clock::time_point s =
160 std::chrono::high_resolution_clock::now();
161 secs =
162 (double) (std::chrono::duration_cast<std::chrono::microseconds>(
163 s - mBegin)
164 .count()) /
165 1000000;
166 }
167 return secs;
168 }
169
174 void setCaption(const std::string& caption) { mCaption = caption; }
175};
176
177} // namespace vcl
178
179#endif // VCL_BASE_TIMER_H
A class representing a box in N-dimensional space.
Definition box.h:46
The Timer class allows to instantiate simple Timer objects that can be used everywhere.
Definition timer.h:42
double delay() const
Returns the time passed between the call of start() and this member function. Works also if the timer...
Definition timer.h:148
void start()
Starts the timer.
Definition timer.h:93
void stopAndPrint()
Stops the timer and prints the time passed between the call of start() and this member function....
Definition timer.h:103
Timer(const char *caption, bool _start=true)
Creates a timer with the given caption. If the given boolean is true, the timer starts.
Definition timer.h:70
Timer(bool _start=true)
Creates a timer with the caption "Timer". If the given boolean is true, the timer starts.
Definition timer.h:56
void setCaption(const std::string &caption)
Sets the caption of the timer.
Definition timer.h:174
void print() const
Prints the time passed between the call of start() and this member function. Works also if the timer ...
Definition timer.h:123
void stop()
Stops the timer.
Definition timer.h:112
Timer(const std::string &caption, bool _start=true)
Creates a timer with the given caption. If the given boolean is true, the timer starts.
Definition timer.h:84