-
Notifications
You must be signed in to change notification settings - Fork 8
/
stopwatch.h
46 lines (38 loc) · 1.09 KB
/
stopwatch.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
#ifndef _STOPWATCH_H_
#define _STOPWATCH_H_
#include <iostream>
#if defined(__linux__)
# include <sys/times.h>
#elif defined(WIN32) || defined(_WIN64)
# include <windows.h>
#endif
class Stopwatch {
public:
Stopwatch(const char* taskname) : taskname_(taskname), start_(now()), last_tick_(now()) {
//std::cout << taskname_ << "..." << std::endl;
}
~Stopwatch() {
double elapsed = now() - start_;
std::cout << taskname_ << ": " << elapsed << "s" << std::endl;
}
void tick(const char* tickname="tick") {
double elapsed = now() - start_;
std::cout << taskname_ << "==>" << tickname << " : delta = " << now() - last_tick_ << "s sum= " << elapsed << std::endl;
last_tick_ = now();
}
static double now() {
#if defined(__linux__)
tms now_tms;
return double(times(&now_tms)) / 100.0;
#elif defined(WIN32) || defined(_WIN64)
return double(GetTickCount()) / 1000.0;
#else
return 0.0;
#endif
}
private:
const char* taskname_;
double start_;
double last_tick_;
};
#endif // _STOPWATCH_H_