-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathtimer.cpp
58 lines (49 loc) · 1.57 KB
/
timer.cpp
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
#include "timer.h"
#include <cstdio>
/* -----------------------------------------------------------------------------
* Initialization of time
* -------------------------------------------------------------------------- */
Timer::Timer()
{
flag = 0;
start();
return;
}
/* -----------------------------------------------------------------------------
* public function, start the timer
* -------------------------------------------------------------------------- */
void Timer::start()
{
t1 = clock();
flag |= 1;
return;
}
/* -----------------------------------------------------------------------------
* public function, stop the timer
* -------------------------------------------------------------------------- */
void Timer::stop()
{
if ( flag&1 ) {
t2 = clock();
flag |= 2;
}
return;
}
/* -----------------------------------------------------------------------------
* public function, print the total time used after timer stops
* -------------------------------------------------------------------------- */
void Timer::print()
{
if ( (flag&3) != 3) return;
cpu_time_used = ((double) (t2 - t1)) / CLOCKS_PER_SEC;
printf("Total CPU time used: %g seconds.\n", cpu_time_used);
return;
}
/* -----------------------------------------------------------------------------
* public function, return the total time used up to now, in seconds
* -------------------------------------------------------------------------- */
double Timer::elapse()
{
if ( (flag&3) != 3) return 0.;
else return ((double) (t2 - t1)) / CLOCKS_PER_SEC;
}