Visual Computing Library
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_MISC_TIMER_H
24#define VCL_MISC_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() { mBegin = std::chrono::high_resolution_clock::now(); }
94
100 {
101 stop();
102 print();
103 }
104
108 void stop()
109 {
110 mEnd = std::chrono::high_resolution_clock::now();
111 mIsStopped = true;
112 }
113
119 void print() const
120 {
121 double secs = delay();
122 int mins = (int) secs / 60;
123 if (mins == 0)
124 std::cout << "[" << secs << " secs]\t" << mCaption << std::endl;
125 else {
126 secs = secs - mins * 60;
127 int hours = mins / 60;
128 if (hours == 0)
129 std::cout << "[" << mins << " mins; " << secs << " secs]\t"
130 << mCaption << std::endl;
131 else {
132 mins = mins - hours * 60;
133 std::cout << "[" << hours << " hours; " << mins << " mins; "
134 << secs << " secs]\t" << mCaption << std::endl;
135 }
136 }
137 }
138
144 double delay() const
145 {
146 double secs;
147 if (mIsStopped) {
148 secs =
149 (double) (std::chrono::duration_cast<std::chrono::microseconds>(
150 mEnd - mBegin)
151 .count()) /
152 1000000;
153 }
154 else {
155 std::chrono::high_resolution_clock::time_point s =
156 std::chrono::high_resolution_clock::now();
157 secs =
158 (double) (std::chrono::duration_cast<std::chrono::microseconds>(
159 s - mBegin)
160 .count()) /
161 1000000;
162 }
163 return secs;
164 }
165
170 void setCaption(const std::string& caption) { mCaption = caption; }
171};
172
173} // namespace vcl
174
175#endif // VCL_MISC_TIMER_H
A class representing a line segment in n-dimensional space. The class is parameterized by a PointConc...
Definition segment.h:43
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:144
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:99
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:170
void print() const
Prints the time passed between the call of start() and this member function. Works also if the timer ...
Definition timer.h:119
void stop()
Stops the timer.
Definition timer.h:108
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