#include <iostream>
#include <ctime>
#include <omp.h>
const int N = 10000000; // 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+CiNpbmNsdWRlIDxvbXAuaD4KCmNvbnN0IGludCBOID0gMTAwMDAwMDA7IC8vIFNpemUgb2YgYXJyYXlzCgp2b2lkIHNlcXVlbnRpYWxBZGQoY29uc3QgaW50KiBhLCBjb25zdCBpbnQqIGIsIGludCogcmVzdWx0LCBpbnQgc2l6ZSkgewogICAgZm9yIChpbnQgaSA9IDA7IGkgPCBzaXplOyBpKyspIHsKICAgICAgICByZXN1bHRbaV0gPSBhW2ldICsgYltpXTsKICAgIH0KfQoKdm9pZCBwYXJhbGxlbEFkZChjb25zdCBpbnQqIGEsIGNvbnN0IGludCogYiwgaW50KiByZXN1bHQsIGludCBzaXplKSB7CiAgICAjcHJhZ21hIG9tcCBwYXJhbGxlbCBmb3IKICAgIGZvciAoaW50IGkgPSAwOyBpIDwgc2l6ZTsgaSsrKSB7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