From c651364527a4b114d94e3a5b32fb0230f757ac28 Mon Sep 17 00:00:00 2001 From: pmfsq5yrv <1159971203@qq.com> Date: Sat, 23 Nov 2024 16:10:03 +0800 Subject: [PATCH] ADD file via upload --- Yang/1.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 Yang/1.c diff --git a/Yang/1.c b/Yang/1.c new file mode 100644 index 0000000..cb92f06 --- /dev/null +++ b/Yang/1.c @@ -0,0 +1,48 @@ +#include +#include +#include + +#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; +}