From 625010cffc96a9b9edd0106cce93a9efd5218eb3 Mon Sep 17 00:00:00 2001 From: pjc97uv2f <1291955673@qq.com> Date: Sat, 23 Nov 2024 11:18:14 +0800 Subject: [PATCH] Add 3 --- 3 | 71 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 3 diff --git a/3 b/3 new file mode 100644 index 0000000..9a31f4e --- /dev/null +++ b/3 @@ -0,0 +1,71 @@ +#include +#include +#include + + +const int SIZE = 1024; + + +void matmul(float** A, float** B, float** C, int n) { + for (int i = 0; i < n; i++) { + for (int j = 0; j < n; j++) { + float sum = 0; + for (int k = 0; k < n; k++) { + sum += A[i][k] * B[k][j]; + } + C[i][j] = sum; + } + } +} + +int main() { + + float** A = new float* [SIZE]; + for (int i = 0; i < SIZE; i++) { + A[i] = new float[SIZE]; + } + + float** B = new float* [SIZE]; + for (int i = 0; i < SIZE; i++) { + B[i] = new float[SIZE]; + } + + float** C = new float* [SIZE]; + for (int i = 0; i < SIZE; i++) { + C[i] = new float[SIZE]; + } + + + for (int i = 0; i < SIZE; i++) { + for (int j = 0; j < SIZE; j++) { + A[i][j] = (float)(rand() % 100); + B[i][j] = (float)(rand() % 100); + } + } + + clock_t start_time, end_time; + + start_time = clock(); + + + matmul(A, B, C, SIZE); + + + end_time = clock(); + + + double elapsed_time = ((double)(end_time - start_time)) / CLOCKS_PER_SEC; + std::cout << "基础矩阵乘法的运行时间:" << elapsed_time << " 秒" << std::endl; + + + for (int i = 0; i < SIZE; i++) { + delete[] A[i]; + delete[] B[i]; + delete[] C[i]; + } + delete[] A; + delete[] B; + delete[] C; + + return 0; +} \ No newline at end of file