-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTimer.h
More file actions
141 lines (106 loc) · 3.07 KB
/
Timer.h
File metadata and controls
141 lines (106 loc) · 3.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// Copyright (C) 2012 - All Rights Reserved
// All rights reserved. http://www.equals-forty-two.com
#pragma once
// WIN
#include <iomanip>
#include <iostream>
#include <Windows.h>
#include <deque>
#include <chrono>
using namespace std;
namespace eh
{
class Timer
{
public:
// 'running' is initially false. A timer needs to be explicitly started
// using 'start' or 'restart'
Timer();
/// Check if the timer is running
bool isRunning() const { return mRunning; }
/// Return the elapsed seconds
double getTime() const;
/// Get the elapsed time on the last batch (start/stop)
double getElapsedTime() const;
/// Init the timer
void reset();
/// Start the timer
void start();
/// Restart the timer
void restart();
/// Stop the timer
void stop();
/// Display the timer state.
void check(std::ostream& os = std::cout);
private:
friend std::ostream& operator<<(std::ostream& os, const Timer& t);
// Data members
bool mRunning;
double mAccumulatedTime; ///> Accumulated time in seconds
LARGE_INTEGER mFrequency;
LARGE_INTEGER mStartTime; ///< Start time of the timer.
};
class DelayTimer : private Timer
{
public:
DelayTimer();
DelayTimer(double delay);
void set(double delay);
bool check() const;
private:
double mDelay;
};
}
class sChrono
{
public:
string name;
eh::Timer _chrono;
// System for averaging FPS over 1 second
vector<double> frameTimes; // All frame durations within the second
chrono::steady_clock::time_point secondStart;
bool secondInitialized = false;
int result = 0;
void NameAndStart(std::string _name)
{
name = _name;
_chrono.restart(); // Reset + start en 1 appel
}
void Stop()
{
_chrono.stop();
// Record the duration of THIS frame
double frameDuration = _chrono.getElapsedTime();
frameTimes.push_back(frameDuration);
// Initialize the first second if not done
if (!secondInitialized)
{
secondStart = chrono::steady_clock::now();
secondInitialized = true;
}
// Check if 1 second has elapsed
auto now = chrono::steady_clock::now();
double elapsedSecond = chrono::duration<double>(now - secondStart).count();
if (elapsedSecond >= 1.0) // Compute and store the result
{
// RESET : new second
secondStart = now;
frameTimes.clear();
frameTimes.push_back(frameDuration);
double sum = 0.0;
for (double t : frameTimes)
sum += t;
double averageSec = sum / frameTimes.size();
result = int(1e6 * averageSec); // us
}
}
int GetMicroSecondes()
{
return result;
}
void Print()
{
int ns = GetMicroSecondes();
cout << name << ": " << ns << " \xc2\xb5s (" << frameTimes.size() << " frames)" << endl;
}
};