ADD file via upload

main
pmfsq5yrv 4 days ago
parent 543d934b57
commit 77b46ac33b

@ -0,0 +1,48 @@
#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;
}
Loading…
Cancel
Save