-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharray_addition.cpp
120 lines (93 loc) · 2.42 KB
/
array_addition.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
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
#include <iostream>
#include <fstream>
#include <iomanip>
#include <stdlib.h>
#include <time.h>
#include <chrono>
#define N_TRIALS 4
// To reduce spikes, each n will be retested N_TRIALS times, and an averege will be performed
#define MIN_SIZE 4
#define MAX_SIZE 22
// They mean n can vary from 2^MIN_SIZE to 2^MAX_SIZE
using namespace std;
float array_addition(int);
float routine1(float*, float*, float*, int);
int main()
{
srand(time(NULL));
ofstream report_file("report_array_addition.csv");
float execution_time;
int i, j;
int n = 1; // 2^0
for (i=0;i<MIN_SIZE-1;++i) n *= 2;
report_file << fixed << setprecision(6);
report_file << "size,time" << endl;
for (i=MIN_SIZE;i<=MAX_SIZE;++i){
n *= 2;
execution_time = 0;
for (j=0;j<N_TRIALS;++j) execution_time += array_addition(n) * (1.0 / N_TRIALS);
//report_file << "size: " << setw(7) << n << ", time: " << execution_time << " s" << endl;
report_file << n << "," << execution_time << endl;
}
report_file.close();
return 0;
}
float array_addition(int n)
{
int i;
float execution_time;
//float A[n], B[n], C[n];
// Note: I first tried with static allocation, but it gives runtime errors when n > 100_000.
float *A, *B, *C;
A = new float[n];
B = new float[n];
C = new float[n];
for (i=0;i<n;++i){
A[i] = (float)rand() / (float)rand();
B[i] = (float)rand() / (float)rand();
}
/* Debug *
cout << fixed << setprecision(5);
cout << "A[" << n << "]:";
for (i=0;i<n;++i){
cout << " " << A[i];
}
cout << endl << endl;
cout << "B[" << n << "]:";
for (i=0;i<n;++i){
cout << " " << B[i];
}
cout << endl << endl;
/**/
execution_time = routine1(A, B, C, n);
/* Debug *
cout << "C[" << n << "]:";
for (i=0;i<n;++i){
cout << " " << C[i];
}
cout << endl << endl;
/**/
delete[] A;
delete[] B;
delete[] C;
/* Debug *
cout << fixed << setprecision(6);
cout << "Execution time: " << execution_time << " s" << endl;
/**/
return execution_time;
}
float routine1 (float* A, float* B, float* C, int n)
{
int i;
float execution_time;
//cout << "For loop start" << endl; // Debug
auto start_time = chrono::high_resolution_clock::now();
for (i=0;i<n;++i){
C[i] = A[i] + B[i];
}
auto end_time = chrono::high_resolution_clock::now();
//cout << "For loop end" << endl << endl; // Debug
auto difference_time = chrono::duration_cast<chrono::microseconds>(end_time - start_time);
execution_time = difference_time.count() * 1e-6;
return execution_time;
}