-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmstopwatch.cxx
81 lines (69 loc) · 1.13 KB
/
mstopwatch.cxx
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
/*
*
*
*/
#include <iostream>
#include <cstdio>
#include <unistd.h>
#include <sys/time.h>
#include <errno.h>
class mStopWatch {
public:
#if 0
mStopWatch();
virtual ~mStopWatch();
#endif
int start();
int period();
int elapse();
protected:
private:
struct timeval at_start;
struct timeval now;
};
#if 0
mStopWatch::mStopWatch()
{
return;
}
mStopWatch::~mStopWatch()
{
std::cout << "# StopWatch destructed" << std::endl;
return;
}
#endif
int mStopWatch::start()
{
int ret = gettimeofday(&at_start, NULL);
if (ret != 0) perror("StopWatch::start");
return ret;
}
int mStopWatch::elapse()
{
int ret = gettimeofday(&now, NULL);
if (ret != 0) {
perror("StopWatch::stop");
return ret;
}
return period();
}
int mStopWatch::period()
{
int dsec = now.tv_sec - at_start.tv_sec;
int dusec = now.tv_usec - at_start.tv_usec;
int diff = dsec * 1000 + (dusec / 1000);
return diff ;
}
#ifdef TEST_MAIN
int main(int argc, char *argv[])
{
mStopWatch sw;
for (int i = 0 ; i < 10 ; i++) {
sw.start();
usleep(i*10000);
int elapse = sw.elapse();
std::cout << "elapse : " << elapse << std::endl;;
}
return 0;
}
#endif