ADD file via upload

main
pecp45i32 1 day ago
parent 36ed28a535
commit 819e081d14

@ -0,0 +1,61 @@
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define SIZE 1024
// 矩阵乘法函数
void matmul(float A[SIZE][SIZE], float B[SIZE][SIZE], float C[SIZE][SIZE], int n) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
C[i][j] = 0;
for (int k = 0; k < n; k++) {
C[i][j] += A[i][k] * B[k][j];
}
}
}
}
int main() {
// 矩阵内存分配
float *A = (float *)malloc(SIZE * SIZE * sizeof(float));
float *B = (float *)malloc(SIZE * SIZE * sizeof(float));
float *C = (float *)malloc(SIZE * SIZE * sizeof(float));
if (A == NULL || B == NULL || C == NULL) {
printf("Memory allocation failed!\n");
return 1;
}
float (*A_matrix)[SIZE] = (float (*)[SIZE])A;
float (*B_matrix)[SIZE] = (float (*)[SIZE])B;
float (*C_matrix)[SIZE] = (float (*)[SIZE])C;
// 初始化
srand(time(NULL));
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
A_matrix[i][j] = rand() % 100;
B_matrix[i][j] = rand() % 100;
}
}
// 计时并输出
clock_t start, end;
start = clock();
matmul(*A_matrix, *B_matrix, *C_matrix, SIZE);
end = clock();
double time_spent = (double)(end - start) / CLOCKS_PER_SEC;
printf("基础矩阵乘法的运行时间为: %lf 秒\n", time_spent);
free(A);
free(B);
free(C);
return 0;
}
Loading…
Cancel
Save