-
Notifications
You must be signed in to change notification settings - Fork 0
/
Timer.cxx
60 lines (57 loc) · 1.38 KB
/
Timer.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
// Timer.cxx
// GL, 14.5.2011
#include "Timer.hxx"
#include <sys/time.h>
#include <sys/resource.h>
#include <unistd.h>
#include <cstdlib>
#include <iostream>
//-----------------------------------
using namespace std;
//-------------------------------------------
// Public parts
//-------------------------------------------
Timer::Timer(): T0(0), T1(0), T(0), running(false){}
//---------------------------------------
Timer::~Timer(){}
//---------------------------------------
void Timer::start(){
if (running){
cout << "Timer is already running!" << endl;
exit(1);
}
running = true;
T0 = doubleTimeSec();
}
//---------------------------------------
void Timer::stop(){
if (!running){
cout << "Tried to stop non-running timer!" << endl;
exit(1);
}
running = false;
T1 = doubleTimeSec();
}
//---------------------------------------
double Timer::getTime(){
if (running == false)
T += T1-T0;
else
T = doubleTimeSec() - T0;
return (T);
}
//---------------------------------------
void Timer::reset(){
running = false;
T0 = 0; T1 = 0; T = 0;
}
//-------------------------------------------
// Private parts
//-------------------------------------------
//-------------------------------------------
double Timer::doubleTimeSec()
{
struct timeval tstruct;
while(gettimeofday(&tstruct,0) == -1);
return double( double(tstruct.tv_sec) + double(tstruct.tv_usec)*1.e-6 );
}