Blue Brain BioExplorer
Timer.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017-2024, EPFL/Blue Brain Project
3  *
4  * This file is part of Blue Brain BioExplorer <https://github.com/BlueBrain/BioExplorer>
5  *
6  * This library is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU Lesser General Public License version 3.0 as published
8  * by the Free Software Foundation.
9  *
10  * This library is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12  * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
13  * details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with this library; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
20 #include "Timer.h"
21 
22 #include <chrono>
23 constexpr double MICRO_PER_SEC = 1000000.0;
24 constexpr double FPS_UPDATE_MILLISECS = 150;
25 
26 namespace core
27 {
29 {
30  _lastFPSTickTime = clock::now();
31  _startTime = clock::now();
32 }
33 
35 {
36  _startTime = clock::now();
37 }
38 
39 double Timer::elapsed() const
40 {
41  return std::chrono::duration<double>{clock::now() - _startTime}.count();
42 }
43 
45 {
46  const auto now = clock::now();
47  _microseconds = std::chrono::duration_cast<std::chrono::microseconds>(now - _startTime).count();
48  _smoothNom = _smoothNom * _smoothingFactor + _microseconds / MICRO_PER_SEC;
49  _smoothDen = _smoothDen * _smoothingFactor + 1.f;
50 
51  const auto secsLastFPSTick = std::chrono::duration_cast<std::chrono::milliseconds>(now - _lastFPSTickTime).count();
52 
53  if (secsLastFPSTick >= FPS_UPDATE_MILLISECS)
54  {
55  _lastFPSTickTime = now;
56  _fps = perSecond();
57  }
58 }
59 
60 int64_t Timer::microseconds() const
61 {
62  return _microseconds;
63 }
64 
65 int64_t Timer::milliseconds() const
66 {
67  return _microseconds / 1000.0;
68 }
69 
70 double Timer::seconds() const
71 {
72  return _microseconds / MICRO_PER_SEC;
73 }
74 
75 double Timer::perSecond() const
76 {
77  return MICRO_PER_SEC / _microseconds;
78 }
79 
81 {
82  return _smoothDen / _smoothNom;
83 }
84 
85 double Timer::fps() const
86 {
87  return _fps;
88 }
89 } // namespace core
constexpr double FPS_UPDATE_MILLISECS
Definition: Timer.cpp:24
constexpr double MICRO_PER_SEC
Definition: Timer.cpp:23
double perSecond() const
Definition: Timer.cpp:75
int64_t microseconds() const
Definition: Timer.cpp:60
double perSecondSmoothed() const
Definition: Timer.cpp:80
void start()
Definition: Timer.cpp:34
void stop()
Definition: Timer.cpp:44
double seconds() const
Definition: Timer.cpp:70
double fps() const
Definition: Timer.cpp:85
int64_t milliseconds() const
Definition: Timer.cpp:65
double elapsed() const
Definition: Timer.cpp:39