-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathc11timer.hxx
More file actions
127 lines (104 loc) · 3.23 KB
/
c11timer.hxx
File metadata and controls
127 lines (104 loc) · 3.23 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
////////////////////////////////////////////////////////////////////
//
// $Id: c11timer.hxx 2021/06/05 13:23:17 kanai Exp $
//
// C++11 timer class based on std::chrono
//
// Copyright (c) 2021 Takashi Kanai
// Released under the MIT license
//
////////////////////////////////////////////////////////////////////
#ifndef _C11TIMER_HXX
#define _C11TIMER_HXX 1
#include <chrono>
#include <ctime>
#include <string>
class C11Timer {
public:
static void ResetCount() {
Single().reset_ = std::chrono::system_clock::now();
};
// リセット後の経過時間のミリ秒で取得(小数点以下)
static double GetNowCount() {
auto diff = std::chrono::system_clock::now() - Single().reset_;
return (double)std::chrono::duration_cast<std::chrono::microseconds>(diff).count() / 1000;
};
// 日付を取得
static void GetDate(std::tm* currentTime) {
::time_t timer;
::time(&timer);
#if defined(_WIN32) || defined(_WIN64)
localtime_s(currentTime, &timer);
#else
std::tm* local = ::localtime(&timer);
if ( local )
*currentTime = *local;
#endif
};
// 現在の日付、時刻を文字列にして返す
static std::string GetDateString() {
/* Not named "time": MSVC CRT #defines time -> _time64 etc., which breaks "std::tm time". */
std::tm tmb;
GetDate( &tmb );
std::string str = std::to_string( tmb.tm_year + 1900 ) + "_"
+ std::to_string( tmb.tm_mon ) + "_"
+ std::to_string( tmb.tm_mday ) + "_"
+ std::to_string( tmb.tm_hour ) + "_"
+ std::to_string( tmb.tm_min ) + "_"
+ std::to_string( tmb.tm_sec );
return str;
};
// FPS計測の更新と取得
static double CheckGetFPS() {
CheckFPS();
return GetFPS();
};
// FPSを取得
static double GetFPS() {
return Single().fps_;
};
// FPSの計測開始
static void ResetFPS() {
Single().fpsCounter_ = std::chrono::system_clock::now();
};
// FPS計測を更新
static void CheckFPS() {
auto diff = std::chrono::system_clock::now() - Single().fpsCounter_;
Single().fps_ = 1000000.0 / (double)std::chrono::duration_cast<std::chrono::microseconds>(diff).count();
// automatically reset fpsCounter_
ResetFPS();
};
// 処理時間の更新と取得
static double CheckGetWatch( bool isReset ) {
CheckWatch( isReset );
return GetWatch();
};
// 処理時間取得
static double GetWatch() {
return Single().watch_;
};
// 処理時間計測開始
static void ResetWatch() {
Single().watchCounter_ = std::chrono::system_clock::now();
};
// 処理時間計測更新
// StartWatch からの経過時間をミリ秒単位で描画
// 続けてDrawWatchする事も可能
static void CheckWatch( bool isReset ) {
auto diff = std::chrono::system_clock::now() - Single().watchCounter_;
Single().watch_ = (double)std::chrono::duration_cast<std::chrono::milliseconds>(diff).count() / 1000.0;
if ( isReset ) ResetWatch();
};
private:
double fps_;
double watch_;
std::chrono::system_clock::time_point reset_;
std::chrono::system_clock::time_point fpsCounter_;
std::chrono::system_clock::time_point watchCounter_;
static C11Timer& Single()
{
static C11Timer single_;
return single_;
};
};
#endif // _C11TIMER_HXX