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