-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCPUTimer.h
97 lines (83 loc) · 2.23 KB
/
CPUTimer.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
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#ifndef CPUTIMER_H
#define CPUTIMER_H
#include <time.h>
#include <iostream>
using namespace std;
// CPUTimer
//
// For ECS 110 - UC Davis - By Ted Krovetz
//
// This class is a convenient way to count CPU time.
// The ANSI C function clock() is used to get the current
// user + system time that the current application
// (and it's child processes) has expended.
// At creation, a variable of CPUTimer type records
// the current value of clock().
//
// reset() - sets the stored variable to the current
// value of clock(). In essence, resetting the clock.
//
// cur_CPUTime() - returns the difference between the current
// clock() value and the previously stored value.
// In essence, returning how much CPU time has passed.
//
// Example: to time a function (possibly main())
//
// #include "CPUTimer"
// void foo(void) {
// CPUTimer ct;
// ... whatever foo does ...
// cerr << ct.cur_CPUTime() << endl;
// }
class CPUTimer {
private:
clock_t tick_count;
public:
CPUTimer(void);
void reset(void);
double cur_CPUTime(void);
};
// AutoCPUTimer
//
// AutoCPUTimer is derived through C++ inheritance. It
// inherits all the public members of CPUTimer, but
// includes a destructor which will automatically
// output the CPU time used to cerr (stderr).
// Example: to time a function (possibly main())
//
// #include "CPUTimer"
// void foo(void) {
// AutoCPUTimer at;
// ... whatever foo does ...
// }
//
// This example will have identical output to the
// previous example, however the output to cerr is
// done automatically,.
class AutoCPUTimer : public CPUTimer {
public:
~AutoCPUTimer(void);
};
// Implementation --
// It is generally not good to expose the mechanics of your ADT
// In the public interface (i.e. the header file). It is here
// however, to make program timing as simple as possible.
// There is _NO_ .cpp file for these classes. #include'ing
// is sufficient for their use.
CPUTimer::CPUTimer(void)
{
tick_count = clock();
}
void CPUTimer::reset(void)
{
tick_count = clock();
}
double CPUTimer::cur_CPUTime(void)
{
return double(clock() - tick_count) / CLOCKS_PER_SEC;
}
AutoCPUTimer::~AutoCPUTimer(void)
{
cerr << cur_CPUTime() << endl;
}
#endif