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 mBegin = std::chrono::high_resolution_clock::now();
95 mIsStopped = false;
96 }
97
103 {
104 stop();
105 print();
106 }
107
111 void stop()
112 {
113 mEnd = std::chrono::high_resolution_clock::now();
114 mIsStopped = true;
115 }
116
122 void print() const
123 {
124 double secs = delay();
125 int mins = (int) secs / 60;
126 if (mins == 0)
127 std::cout << "[" << secs << " secs]\t" << mCaption << std::endl;
128 else {
129 secs = secs - mins * 60;
130 int hours = mins / 60;
131 if (hours == 0)
132 std::cout << "[" << mins << " mins; " << secs << " secs]\t"
133 << mCaption << std::endl;
134 else {
135 mins = mins - hours * 60;
136 std::cout << "[" << hours << " hours; " << mins << " mins; "
137 << secs << " secs]\t" << mCaption << std::endl;
138 }
139 }
140 }
141
147 double delay() const
148 {
149 double secs;
150 if (mIsStopped) {
151 secs =
152 (double) (std::chrono::duration_cast<std::chrono::microseconds>(
153 mEnd - mBegin)
154 .count()) /
155 1000000;
156 }
157 else {
158 std::chrono::high_resolution_clock::time_point s =
159 std::chrono::high_resolution_clock::now();
160 secs =
161 (double) (std::chrono::duration_cast<std::chrono::microseconds>(
162 s - mBegin)
163 .count()) /
164 1000000;
165 }
166 return secs;
167 }
168
173 void setCaption(const std::string& caption) { mCaption = caption; }
174};
175
176} // namespace vcl
177
178#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:147
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:102
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:173
void print() const
Prints the time passed between the call of start() and this member function. Works also if the timer ...
Definition timer.h:122
void stop()
Stops the timer.
Definition timer.h:111
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