-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtimer.h
More file actions
66 lines (52 loc) · 1.3 KB
/
timer.h
File metadata and controls
66 lines (52 loc) · 1.3 KB
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
/* $Id: timer.h 93 2008-04-11 01:11:38Z wangsl $ */
#ifndef TIMER_H
#define TIMET_H
#include <iostream>
using namespace std;
#include <unistd.h>
#include <ctime>
#include <sys/times.h>
class Timer
{
public:
Timer()
{
times(&start_time);
ticks_per_second = sysconf(_SC_CLK_TCK);
}
~Timer() { }
void reset() { times(&start_time); }
double time() const
{
struct tms end_time;
times(&end_time);
clock_t diff = end_time.tms_utime - start_time.tms_utime;
double seconds = ((double) diff)/ticks_per_second;
return seconds;
}
static void print_time(const double &sec)
{
const streamsize default_precision = cout.precision();
cout.precision(2);
if(sec < 60)
cout << " " << fixed << sec << " secs";
else
if(sec < 3600)
cout << " " << int(sec/60) << " mins, " << sec-int(sec/60)*60 << " secs";
else
if(sec < 86400)
cout << " " << int(sec/3600) << " hrs, "
<< (sec-int(sec/3600)*3600)/60.0 << " mins";
else
cout << " " << int(sec/86400) << " days, "
<< (sec-int(sec/86400)*86400)/3600.0 << " hrs";
cout << endl;
cout.precision(default_precision);
}
private:
struct tms start_time;
int ticks_per_second;
};
void reset_timer();
double time();
#endif /* TIMER_H */