#include <iostream>
#include <ctime>
#include <omp.h>
const int N = 100000000; // Size of arrays
void sequentialAdd(const int* a, const int* b, int* result, int size) {
for (int i = 0; i < size; i++) {
result[i] = a[i] + b[i];
}
}
void parallelAdd(const int* a, const int* b, int* result, int size) {
#pragma omp parallel for
for (int i = 0; i < size; i++) {
result[i] = a[i] + b[i];
}
}
int main() {
// Allocate dynamic arrays
int* a = new int[N];
int* b = new int[N];
int* result = new int[N];
// Initialize arrays with values
for (int i = 0; i < N; ++i) {
a[i] = 1;
b[i] = 2;
}
// Sequential addition and timing
clock_t startSeq = clock();
sequentialAdd(a, b, result, N);
clock_t endSeq = clock();
double timeSeq = double(endSeq - startSeq) / CLOCKS_PER_SEC;
std::cout << "Sequential addition time: " << timeSeq << " seconds\n";
// Parallel addition and timing
clock_t startPar = clock();
parallelAdd(a, b, result, N);
clock_t endPar = clock();
double timePar = double(endPar - startPar) / CLOCKS_PER_SEC;
std::cout << "Parallel addition time: " << timePar << " seconds\n";
// Clean up dynamic arrays
delete[] a;
delete[] b;
delete[] result;
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8Y3RpbWU+CiNpbmNsdWRlIDxvbXAuaD4KCmNvbnN0IGludCBOID0gMTAwMDAwMDAwOyAvLyBTaXplIG9mIGFycmF5cwoKdm9pZCBzZXF1ZW50aWFsQWRkKGNvbnN0IGludCogYSwgY29uc3QgaW50KiBiLCBpbnQqIHJlc3VsdCwgaW50IHNpemUpIHsKICAgIGZvciAoaW50IGkgPSAwOyBpIDwgc2l6ZTsgaSsrKSB7CiAgICAgICAgcmVzdWx0W2ldID0gYVtpXSArIGJbaV07CiAgICB9Cn0KCnZvaWQgcGFyYWxsZWxBZGQoY29uc3QgaW50KiBhLCBjb25zdCBpbnQqIGIsIGludCogcmVzdWx0LCBpbnQgc2l6ZSkgewogICAgI3ByYWdtYSBvbXAgcGFyYWxsZWwgZm9yCiAgICBmb3IgKGludCBpID0gMDsgaSA8IHNpemU7IGkrK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