-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchrono.h
executable file
·70 lines (59 loc) · 1.83 KB
/
chrono.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
/*
* File: chrono.h
* Author: Sajeeb
*
* Created on October 26, 2017, 5:02 PM
*/
/*
Description:
This is a custom chrono class used to measure the amount of time a
particular operation, or a particular statement took to complete.
The amount of time it took an operation or a series of operations
to complete can be found out by wrapping the operation or the series
of operations with the chrono class. In other words calling the
reset_duration() of the chrono object before the start of the
operation(s) and doing the following right after the operation
statements:
cout<< "Operation(s) Took: " << chrono_object.calculateDuration();
Every time the above statement is called the clock in the chrono class
is reset from the creation of the chrono object to the time of calling
the above statement. The chrono class returns the time in milliseconds.
*/
#ifndef CHRONO_H
#define CHRONO_H
#include <iostream>
#include <math.h>
#include <cmath>
#include <chrono>
using std::cout;
using std::endl;
class chrono {
public:
chrono() {
reset();
if (debug) {
cout<< "Constructor for chrono called: " << duration <<endl;
}
}
~chrono() {
if (debug) {
cout<< "Destructor for chrono called: " << duration <<endl;
}
}
long calculateDuration() {
long old_duration = duration;
reset();
return (duration-old_duration);
}
void reset() {
auto now = std::chrono::system_clock::now();
auto now_ms = std::chrono::time_point_cast<std::chrono::milliseconds>(now);
auto epoch = now_ms.time_since_epoch();
auto value = std::chrono::duration_cast<std::chrono::milliseconds>(epoch);
duration = value.count();
}
long duration;
private:
static const bool debug = false;
};
#endif /* CHRONO_H */