forked from X-DataInitiative/tick
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathperformance_test.cpp
More file actions
208 lines (169 loc) · 5.21 KB
/
performance_test.cpp
File metadata and controls
208 lines (169 loc) · 5.21 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
// License: BSD 3 clause
//
// Created by Martin Bompaire on 22/05/15.
//
#include "tick/array_test/performance_test.h"
using std::cout;
using std::endl;
double test_sum_double_pointer(ulong size, ulong n_loops) {
double* values = new double[size];
for (ulong i = 0; i < size; i++) values[i] = 1;
double sum = 0;
for (ulong j = 0; j < n_loops; j++) {
for (ulong i = 0; i < size; i++) sum += values[i];
}
delete[] values;
return sum;
}
double test_sum_ArrayDouble(ulong size, ulong n_loops) {
ArrayDouble array = ArrayDouble(size);
array.fill(1);
double sum = 0;
for (ulong j = 0; j < n_loops; j++) {
for (ulong i = 0; i < size; i++) sum += array[i];
}
return sum;
}
double test_sum_SArray_shared_ptr(ulong size, ulong n_loops) {
SArrayDoublePtr sarray = SArrayDouble::new_ptr(size);
sarray->fill(1);
double sum = 0;
for (ulong j = 0; j < n_loops; j++) {
for (ulong i = 0; i < size; i++) sum += (*sarray)[i];
}
return sum;
}
double test_sum_VArray_shared_ptr(ulong size, ulong n_loops) {
VArrayDoublePtr varray = VArrayDouble::new_ptr(size);
varray->fill(1);
double sum = 0;
for (ulong j = 0; j < n_loops; j++) {
for (ulong i = 0; i < size; i++) sum += (*varray)[i];
}
return sum;
}
// Testing array_double accessing directly to the pointer
// versus the inline [] method
void test_element_access() {
ulong size = 50000;
ulong nLoops = 10000;
cout << "*** Testing accessing elements of ArrayDouble and VArrayDouble"
<< endl;
double* values = new double[size];
for (ulong i = 0; i < size; i++) values[i] = 1;
ArrayDouble array = ArrayDouble(size, values);
array.print();
double sum = 0;
double* val = array.data();
START_TIMER(1, "fast_array");
for (ulong j = 0; j < nLoops; j++) {
for (ulong i = 0; i < array.size(); i++) sum += val[i];
}
END_TIMER(1);
cout << "sum = " << sum << endl;
sum = 0;
START_TIMER(2, "slow_array");
for (ulong j = 0; j < nLoops; j++) {
for (ulong i = 0; i < array.size(); i++) sum += array[i];
}
END_TIMER(2);
cout << "sum = " << sum << endl;
cout << "-------------" << endl;
cout << "intial data=" << values << endl;
sum = 0;
VArrayDoublePtr vv = VArrayDouble::new_ptr(size);
for (ulong i = 0; i < size; i++) (*vv)[i] = values[i];
cout << "created " << vv << endl;
START_TIMER(4, "varray_shared_ptr");
for (ulong j = 0; j < nLoops; j++) {
for (ulong i = 0; i < vv->size(); i++) sum += (*vv)[0];
}
END_TIMER(4);
cout << "sum = " << sum << endl;
COMPARE_TIMER(1, 2);
COMPARE_TIMER(1, 4);
delete[] values;
}
double inherited_func(InheritedArray* array, ulong* nLoops) {
double sum = 0;
for (ulong j = 0; j < *nLoops; j++) {
for (ulong i = 0; i < array->size; i++) sum += (*array)[i];
}
return sum;
}
double abstract_func(ToyAbstractArray* array, ulong* nLoops) {
double sum = 0.0;
for (ulong j = 0; j < *nLoops; j++) {
for (ulong i = 0; i < array->size; i++) sum += array->getValue(i);
}
return sum;
}
double inherited_func_no_ptr(InheritedArray array, ulong nLoops) {
double sum = 0.0;
for (ulong j = 0; j < nLoops; j++) {
for (ulong i = 0; i < array.size; i++) sum += array[i];
}
return sum;
}
// Testing array_double accessing directly to the pointer
// versus the inline [] method
void test_element_access_inherited_array() {
ulong size = 50000;
ulong nLoops = 10000;
cout << "\n*** Testing accessing elements from inherited Array " << endl;
double* values = new double[size];
for (ulong i = 0; i < size; i++) values[i] = 1;
ArrayDouble array = ArrayDouble(size, values);
double sum = 0;
START_TIMER(1, "direct_array");
for (ulong j = 0; j < nLoops; j++) {
for (ulong i = 0; i < array.size(); i++) sum += array[i];
}
END_TIMER(1);
cout << "sum = " << sum << endl;
InheritedArray inheritedArray = InheritedArray(values, size);
InheritedArray* inheritedArrayPtr = &inheritedArray;
ToyAbstractArray* abstractArrayPtr = &inheritedArray;
sum = 0;
START_TIMER(5, "inherited_array");
for (ulong j = 0; j < nLoops; j++) {
for (ulong i = 0; i < inheritedArray.size; i++) sum += inheritedArray[i];
}
END_TIMER(5);
cout << "sum = " << sum << endl;
sum = 0;
START_TIMER(6, "inherited_array_in_function");
sum = inherited_func_no_ptr(inheritedArray, nLoops);
END_TIMER(6);
cout << "sum = " << sum << endl;
sum = 0;
START_TIMER(2, "inherited_array_ptr");
for (ulong j = 0; j < nLoops; j++) {
for (ulong i = 0; i < inheritedArrayPtr->size; i++)
sum += (*inheritedArrayPtr)[i];
}
END_TIMER(2);
cout << "sum = " << sum << endl;
sum = 0;
START_TIMER(8, "abstract_array_ptr");
for (ulong j = 0; j < nLoops; j++) {
for (ulong i = 0; i < abstractArrayPtr->size; i++)
sum += abstractArrayPtr->getValue(i);
}
END_TIMER(8);
cout << "sum = " << sum << endl;
START_TIMER(3, "inherited_array_in_function_ptr");
sum = inherited_func(&inheritedArray, &nLoops);
END_TIMER(3);
cout << "sum = " << sum << endl;
START_TIMER(4, "abstract_array_in_function_ptr");
sum = abstract_func(&inheritedArray, &nLoops);
END_TIMER(4);
cout << "sum = " << sum << endl;
COMPARE_TIMER(1, 5);
COMPARE_TIMER(1, 6);
COMPARE_TIMER(5, 2);
COMPARE_TIMER(2, 3);
COMPARE_TIMER(2, 4);
delete[] values;
}