-
Notifications
You must be signed in to change notification settings - Fork 0
/
srcMeasure.h
79 lines (60 loc) · 1.88 KB
/
srcMeasure.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
70
71
72
73
74
75
76
77
78
79
#ifndef SRC_MEASURE_H
#define SRC_MEASURE_H
#include <iostream>
#include <chrono>
#include <cmath>
#include <string>
#define NUMBER_OF_TIMER 10
#define DECIMAL 10
using namespace std;
class SrcMeasure {
private:
// Chrono supported from C++ 11
chrono::time_point<chrono::steady_clock> start_time[NUMBER_OF_TIMER];
chrono::time_point<chrono::steady_clock> end_time[NUMBER_OF_TIMER];
public:
SrcMeasure() {
}
// Start the timerIndex th timer
void startTime(int timerIndex) {
if (DEBUG_M)
start_time[timerIndex] = chrono::high_resolution_clock::now();
}
// Return elapsed time of timerIndex th timer
dtype endTime(int timerIndex, string s) {
if (DEBUG_M) {
end_time[timerIndex] = chrono::high_resolution_clock::now();
long long nsTimeCount = chrono::duration_cast<chrono::nanoseconds>(end_time[timerIndex] - start_time[timerIndex]).count();
dtype elapsedTime = nsTimeCount / pow(10, 9);
cout << "[" << s << "] Time : " << f_to_s(elapsedTime, 8, 6, -6) << endl;
return elapsedTime;
}
return 0;
}
string f_to_s(dtype floatingNumber, int length, int precision, int floor) {
char* fChar = new char[length + 1];
float fractionalPart = floatingNumber - (long)floatingNumber;
int _length = length;
int _precision = precision;
string _expression = "lf";
floatingNumber = floatingNumber / pow(DECIMAL, floor);
floatingNumber = long long(floatingNumber);
floatingNumber *= pow(DECIMAL, floor);
if (abs(floatingNumber) >= pow(DECIMAL, length)) {
_length = length - 3;
_precision = length - 7;
_expression = "e";
}
else if (abs(floatingNumber) >= pow(DECIMAL, length - 2)) {
_precision = 0;
}
if (floor >= 0)
_precision = 0;
string format = "%" + to_string(_length) + "." + to_string(_precision) + _expression;
snprintf(fChar, length + 1, format.c_str(), floatingNumber);
string out(fChar);
delete[] fChar;
return out;
}
};
#endif