-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadd_arrays.cpp
63 lines (45 loc) · 1.54 KB
/
add_arrays.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
#include <cstdlib>
#include <iostream>
#include <chrono>
#include <string>
using namespace std;
using namespace std::chrono;
void print_array(float* array, int size, const char* name) {
cout << name << " : " << "size : " << size << endl;
for (int i = 0; i < size; i++) {
cout << array[i] << " ";
}
cout << endl;
}
void add_arrays(float* array_a, float* array_b, float* array_c, int size) {
for (int i = 0; i < size; i++) {
array_c[i] = array_a[i] + array_b[i];
}
}
int main(int argc, char* argv[]) {
std::cout << "Number of arguments: " << argc << std::endl;
std::cout << "Arguments:" << std::endl;
for (int i = 0; i < argc; ++i) {
std::cout << "argv[" << i << "]: " << argv[i] << std::endl;
}
int k = stoi(argv[1]);
int size = k * 1000000;
float* array_a = (float*)malloc(size * sizeof(float));
float* array_b = (float*)malloc(size * sizeof(float));
float* array_c = (float*)malloc(size * sizeof(float));
for (int i = 0; i < size; i++) {
array_a[i] = i;
array_b[i] = size - 1 - i;
array_c[i] = 0;
}
auto start = high_resolution_clock::now();
add_arrays(array_a, array_b, array_c, size);
auto stop = high_resolution_clock::now();
auto duration = duration_cast<milliseconds>(stop - start);
cout << "Time taken for K = " << k << " million elements: " << duration.count() << " milliseconds" << endl;
// print_array(array_c, size, "array_c");
free(array_a);
free(array_b);
free(array_c);
return 0;
}