forked from kentavv/Albert
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprofile.h
More file actions
82 lines (63 loc) · 1.56 KB
/
profile.h
File metadata and controls
82 lines (63 loc) · 1.56 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
#ifndef _PROFILE_H_
#define _PROFILE_H_
#include <string>
#include <stdio.h>
#include <time.h>
#ifndef WIN32
#include <sys/time.h>
#else
#include <sys/timeb.h>
typedef int suseconds_t;
struct timeval {
time_t tv_sec;
suseconds_t tv_usec;
};
inline int gettimeofday(struct timeval *tv, void * /*tz*/) {
struct _timeb tb;
_ftime(&tb);
tv->tv_sec = tb.time;
tv->tv_usec = tb.millitm * 1000;
return 0;
}
#endif
struct Profile {
Profile(const char *str) : rank(-1), name(str) {
restart();
}
Profile(int rank_, const char *str) : rank(rank_), name(str) {
restart();
}
~Profile() {
if (!stopped_) report();
}
void restart() {
gettimeofday(&tv_start, nullptr);
stopped_ = false;
}
void stop() {
stopped_ = true;
}
void setRank(int rank_) {
rank = rank_;
}
void report() {
struct timeval tv_end;
gettimeofday(&tv_end, nullptr);
double dt = (tv_end.tv_sec + tv_end.tv_usec / 1e6) - (tv_start.tv_sec + tv_start.tv_usec / 1e6);
if (rank < 0) {
printf("%s: (profile) %f s\n", name.c_str(), dt);
//qDebug("%s: (profile) %f s\n", name.c_str(), dt);
} else {
printf("<%d> %s: (profile) %f s\n", rank, name.c_str(), dt);
//qDebug("<%d> %s: (profile) %f s\n", rank, name.c_str(), dt);
}
}
int rank;
std::string name;
struct timeval tv_start;
bool stopped_;
protected:
Profile(const Profile &);
Profile operator=(const Profile &);
};
#endif