-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclock.h
64 lines (58 loc) · 1.63 KB
/
clock.h
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
#pragma once
#include <string>
#include <chrono>
#include <cstdio>
#include <cstdarg>
using namespace std;
using namespace std::chrono;
class Clock {
public:
explicit Clock(const string &str) {
s = str;
}
const char *start() {
passed_time = 0.0;
paused = false;
start_time = duration_cast<microseconds>(high_resolution_clock::now().time_since_epoch()).count();
sprintf(str, "[\033[1m\033[44;30m%-12s\033[0m] Start...", s.c_str());
return str;
}
const char *count(const char *fmt = "", ...) {
va_list args;
char str2[1000];
va_start(args, fmt);
vsprintf(str2, fmt, args);
va_end(args);
uint64_t end_time = duration_cast<microseconds>(high_resolution_clock::now().time_since_epoch()).count();
double t = double(end_time - start_time) / 1e6;
if (paused) {
t = passed_time;
} else {
t += passed_time;
}
sprintf(str, "[\033[1m\033[44;31m%-12s\033[0m] %.6lfs %s", s.c_str(), t, str2);
return str;
}
const char *pause() {
if (!paused) {
uint64_t end_time = duration_cast<microseconds>(high_resolution_clock::now().time_since_epoch()).count();
double t = double(end_time - start_time) / 1e6;
passed_time += t;
paused = true;
}
return str;
}
const char *goon() {
if (paused) {
start_time = duration_cast<microseconds>(high_resolution_clock::now().time_since_epoch()).count();
paused = false;
}
return str;
}
private:
char str[1000]{};
string s{};
double passed_time;
uint64_t start_time{};
bool paused;
};