Line | |
---|
1 | #ifndef SOMETIME_H |
---|
2 | #define SOMETIME_H |
---|
3 | |
---|
4 | #include<cstdlib> |
---|
5 | #include <sys/time.h> // To seed random generator |
---|
6 | |
---|
7 | using namespace std; |
---|
8 | |
---|
9 | extern bool diffTimes(timeval& ret, const timeval &tLater, const timeval &tEarlier); |
---|
10 | |
---|
11 | class CStepTime |
---|
12 | { |
---|
13 | static int timeVal; |
---|
14 | |
---|
15 | public: |
---|
16 | |
---|
17 | static void makeStart() |
---|
18 | { |
---|
19 | timeVal = 0; |
---|
20 | } |
---|
21 | |
---|
22 | static int getTime() |
---|
23 | { |
---|
24 | return timeVal; |
---|
25 | } |
---|
26 | |
---|
27 | static void stepTime() |
---|
28 | { |
---|
29 | timeVal++; |
---|
30 | } |
---|
31 | }; |
---|
32 | |
---|
33 | |
---|
34 | |
---|
35 | class CStopWatch |
---|
36 | { |
---|
37 | timeval timeStart; |
---|
38 | timeval timeStop; |
---|
39 | |
---|
40 | long int timeBound; |
---|
41 | |
---|
42 | public: |
---|
43 | |
---|
44 | CStopWatch() {} |
---|
45 | ~CStopWatch(){} |
---|
46 | |
---|
47 | bool timeBoundBroken() |
---|
48 | { |
---|
49 | timeval actTime; |
---|
50 | gettimeofday(&actTime,NULL); |
---|
51 | |
---|
52 | return actTime.tv_sec - timeStart.tv_sec > timeBound; |
---|
53 | } |
---|
54 | |
---|
55 | bool markStartTime() |
---|
56 | { |
---|
57 | return gettimeofday(&timeStart,NULL) == 0; |
---|
58 | } |
---|
59 | |
---|
60 | bool markStopTime() |
---|
61 | { |
---|
62 | return gettimeofday(&timeStop,NULL) == 0; |
---|
63 | } |
---|
64 | |
---|
65 | |
---|
66 | void setTimeBound(long int seconds) |
---|
67 | { |
---|
68 | timeBound = seconds; |
---|
69 | } |
---|
70 | |
---|
71 | long int getTimeBound() |
---|
72 | { |
---|
73 | return timeBound; |
---|
74 | } |
---|
75 | |
---|
76 | double getElapsedTime() |
---|
77 | { |
---|
78 | timeval r; |
---|
79 | double retT; |
---|
80 | diffTimes(r,timeStop, timeStart); |
---|
81 | |
---|
82 | retT = r.tv_usec; |
---|
83 | retT /= 1000000.0; |
---|
84 | retT += (double)r.tv_sec; |
---|
85 | return retT; |
---|
86 | } |
---|
87 | |
---|
88 | unsigned int getElapsedusecs() |
---|
89 | { |
---|
90 | unsigned int retT; |
---|
91 | timeval r; |
---|
92 | |
---|
93 | diffTimes(r,timeStop, timeStart); |
---|
94 | |
---|
95 | retT = r.tv_usec; |
---|
96 | |
---|
97 | retT += r.tv_sec * 1000000; |
---|
98 | return retT; |
---|
99 | } |
---|
100 | }; |
---|
101 | |
---|
102 | #endif |
---|
Note: See
TracBrowser
for help on using the repository browser.