-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChrono.cpp
More file actions
124 lines (105 loc) · 2.34 KB
/
Chrono.cpp
File metadata and controls
124 lines (105 loc) · 2.34 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
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include"Chrono.hpp"
#include <iostream>
#include <time.h>
#include<string>
std::ostream& operator << (std::ostream& c,const Chrono& cht)
{
size_t hours=0, minutes=0,seconds=0;
if(cht.counting)
{
hours=9999999999, minutes=59,seconds=59;
}
else
{
time_t t_diff=cht.t_elapsed; // I copy the value if I want to look at partial times
if(t_diff>=3600)
{
hours=t_diff/3600;
t_diff=t_diff-hours*3600;
}
if(t_diff>=60)
{
minutes=t_diff/60;
t_diff=t_diff-minutes*60;
}
seconds=t_diff;
}
double cputime=(static_cast<double>(cht.t_clock_elapsed))/CLOCKS_PER_SEC;
c<<hours<<":"<<std::setw(2)<<std::right<<std::setfill('0')<<minutes<<":"<<std::setw(2)<<std::right<<std::setfill('0')<<seconds<<" (CPU's "<<cputime<<" s )";
return c;
};
Chrono::Chrono()
:counting(false),
t_start(0),
t_end(0),
t_elapsed(0),
t_clock_start(0),
t_clock_end(0),
t_clock_elapsed(0)
{}
Chrono::~Chrono()
{
reset();
}
void Chrono::start()
{
if(~counting)
{
time(&t_start);
t_clock_start = clock();
t_end=t_start;
t_clock_end=t_clock_start;
counting=true;
}
else
{
std::cout<<"CHRONO: WARNING: NO START SINCE COUNTER WAS PREVIOUSLY STARTED"<<std::endl;
}
}
void Chrono::stop()
{
if(counting)
{
time(&t_end);
t_clock_end = clock();
t_elapsed=t_elapsed+difftime(t_end,t_start);
t_clock_elapsed=t_clock_elapsed+(t_clock_end-t_clock_start);
counting=false;
}
}
void Chrono::reset()
{
this->stop();
counting=false;
t_start=0;
t_end=0;
t_elapsed=0;
t_clock_start=0;
t_clock_end=0;
t_clock_elapsed=0;
}
void Chrono::printElapsedTime()
{
if(counting)
{
std::cout<<"CHRONO: WARNING: YOU HAVE TO STOP CHRONOMETER BEFORE EVALUATING ELAPSED TIME"<<std::endl;
}
else
{
time_t t_diff=t_elapsed; // I copy the value if I want to look at partial times
size_t hours=0, minutes=0,seconds=0;
if(t_diff>=3600)
{
hours=t_diff/3600;
t_diff=t_diff-hours*3600;
}
if(t_diff>=60)
{
minutes=t_diff/60;
t_diff=t_diff-minutes*60;
}
seconds=t_diff;
double cputime=(static_cast<double>(t_clock_elapsed))/CLOCKS_PER_SEC;
std::cout<<hours<<":"<<std::setw(2)<<std::right<<std::setfill('0')<<minutes<<":"<<std::setw(2)<<std::right<<std::setfill('0')<<seconds<<" (CPU's "<<cputime<<" s )";
}
}