-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.cpp
94 lines (84 loc) · 3.33 KB
/
example.cpp
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
#include <array>
#include <chrono>
#include <fstream>
#include <iostream>
#include <cmath>
#include <vector>
#include "CubicSpline.h"
int main() {
std::vector<double> x, y;
std::array<double, 110> xArray, yArray;
double xArr[110], yArr[110];
int j = 0;
for(double i = -2.5; i <= 8.5; i += 0.1) {
x.push_back(i);
y.push_back(sin(2.*cos(3.*i)));
if(j < 110) {
xArray[j] = i;
yArray[j] = sin(2.*cos(3.*i));
xArr[j] = i;
yArr[j] = sin(2.*cos(3.*i));
j++;
}
}
int iter = 5000;
std::cout << "Using " << iter << " iterations" << std::endl << std::endl;
std::cout << "New Spline Class:" << std::endl;
std::cout << " Vectors:" << std::endl;
double totalSetup = 0;
double totalEval = 0;
double diff;
int num;
for(int i = 0; i < iter; i++) {
auto t1 = std::chrono::high_resolution_clock::now();
CubicSpline f(x, y);
auto t2 = std::chrono::high_resolution_clock::now();
diff = 0.0;
num = 0;
for(double i = -1.; i <= 5.0; i += 0.0034) {
diff += fabs(sin(2.*cos(3.*i)) - f(i));
num++;
}
auto t3 = std::chrono::high_resolution_clock::now();
totalSetup += std::chrono::duration_cast<std::chrono::microseconds>(t2 - t1).count();
totalEval += std::chrono::duration_cast<std::chrono::microseconds>(t3 - t2).count();
}
std::cout << " Avg. Setup Time: " << totalSetup/iter << " us. Avg. Eval Time for "<< num << " calls: " << totalEval/iter << " us. Avg. diff: " << diff/num << std::endl;
std::cout << " std::Array:" << std::endl;
totalSetup = 0;
totalEval = 0;
for(int i = 0; i < iter; i++) {
auto t1 = std::chrono::high_resolution_clock::now();
CubicSpline f(xArray, yArray);
auto t2 = std::chrono::high_resolution_clock::now();
diff = 0.0;
num = 0;
for(double i = -1.; i <= 5.0; i += 0.0034) {
diff += fabs(sin(2.*cos(3.*i)) - f(i));
num++;
}
auto t3 = std::chrono::high_resolution_clock::now();
totalSetup += std::chrono::duration_cast<std::chrono::microseconds>(t2 - t1).count();
totalEval += std::chrono::duration_cast<std::chrono::microseconds>(t3 - t2).count();
}
std::cout << " Avg. Setup Time: " << totalSetup/iter << " us. Avg. Eval Time for "<< num << " calls: " << totalEval/iter << " us. Avg. diff: " << diff/num << std::endl;
std::cout << " Array:" << std::endl;
totalSetup = 0;
totalEval = 0;
for(int i = 0; i < iter; i++) {
auto t1 = std::chrono::high_resolution_clock::now();
CubicSpline f(xArr, yArr);
auto t2 = std::chrono::high_resolution_clock::now();
diff = 0.0;
num = 0;
for(double i = -1.; i <= 5.0; i += 0.0034) {
diff += fabs(sin(2.*cos(3.*i)) - f(i));
num++;
}
auto t3 = std::chrono::high_resolution_clock::now();
totalSetup += std::chrono::duration_cast<std::chrono::microseconds>(t2 - t1).count();
totalEval += std::chrono::duration_cast<std::chrono::microseconds>(t3 - t2).count();
}
std::cout << " Avg. Setup Time: " << totalSetup/iter << " us. Avg. Eval Time for "<< num << " calls: " << totalEval/iter << " us. Avg. diff: " << diff/num << std::endl;
return 0;
}