-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathTimeRecord.cuh
More file actions
73 lines (61 loc) · 1.75 KB
/
TimeRecord.cuh
File metadata and controls
73 lines (61 loc) · 1.75 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
//
// Created by gxl on 2021/3/29.
//
#ifndef PTGRAPH_TIMERECORD_CUH
#define PTGRAPH_TIMERECORD_CUH
#pragma once
#include <chrono>
#include <iostream>
#include <typeinfo>
#include <string>
#include <utility>
using namespace std;
template<class TimeUnit>
class TimeRecord {
private:
chrono::time_point<chrono::steady_clock, chrono::nanoseconds> startTime;
long duration{};
bool isStart = false;
string recordName = "TimeRecord";
public:
TimeRecord() {}
// explicit TimeRecord(string name) : recordName(std::move(name)) {
// };
explicit TimeRecord(string name){
this->recordName = name;
}
bool _isStart(){
return isStart;
}
void startRecord() {
isStart = true;
startTime = chrono::steady_clock::now();
}
void endRecord() {
if (isStart) {
duration += chrono::duration_cast<TimeUnit>(chrono::steady_clock::now() - startTime).count();
isStart = false;
} else {
duration = 0;
//printf(" No start record! this reocrd %s is 0! \n",recordName);
cout<<recordName<<" did not start"<<endl;
}
}
void print() {
if (typeid(TimeUnit) == typeid(chrono::milliseconds)) {
cout << recordName << " time is " << duration << " ms ";
} else if (typeid(TimeUnit) == typeid(chrono::microseconds)) {
cout << recordName << " time is " << duration << " micro++s ";
} else if (typeid(TimeUnit) == typeid(chrono::nanoseconds)) {
cout << recordName << " time is " << duration << " ns ";
}
}
void clearRecord() {
isStart = false;
duration = 0;
}
long getDuration(){
return duration;
}
};
#endif //PTGRAPH_TIMERECORD_CUH