forked from pi7mcrg2k/operator_optimization
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
49 lines
1.1 KiB
49 lines
1.1 KiB
4 weeks ago
|
#include <stdio.h>
|
||
|
#include <stdlib.h>
|
||
|
#include <time.h>
|
||
|
|
||
|
#define SIZE 10000000
|
||
|
|
||
|
void vectorAdd(float* A, float* B, float* C, int size) {
|
||
|
int i;
|
||
|
for (i = 0; i < size; ++i) {
|
||
|
C[i] = A[i] + B[i];
|
||
|
}
|
||
|
}
|
||
|
|
||
|
int main() {
|
||
|
float* A = (float*)malloc(SIZE * sizeof(float));
|
||
|
float* B = (float*)malloc(SIZE * sizeof(float));
|
||
|
float* C = (float*)malloc(SIZE * sizeof(float));
|
||
|
if (A == NULL || B == NULL || C == NULL) {
|
||
|
return 1;
|
||
|
}
|
||
|
|
||
|
// float A[SIZE], B[SIZE], C[SIZE];
|
||
|
srand((unsigned)time(NULL));
|
||
|
int i;
|
||
|
for (i = 0; i < SIZE; ++i) {
|
||
|
A[i] = rand() % 100;
|
||
|
B[i] = rand() % 100;
|
||
|
}
|
||
|
|
||
|
|
||
|
clock_t start_time = clock();
|
||
|
|
||
|
vectorAdd(A, B, C, SIZE);
|
||
|
|
||
|
clock_t end_time = clock();
|
||
|
double time = (double)(end_time - start_time) / CLOCKS_PER_SEC;
|
||
|
|
||
|
printf("%f\n", time);
|
||
|
|
||
|
// for (i = 0; i < SIZE; ++i) {
|
||
|
// printf("A[%d] + B[%d] = C[%d] -> %f + %f = %f\n", i, i, i, A[i], B[i], C[i]);
|
||
|
// }
|
||
|
|
||
|
free(A);
|
||
|
free(B);
|
||
|
free(C);
|
||
|
return 0;
|
||
|
}
|