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.

60 lines
1.4 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#include <stdio.h>
#include <ctime>
#include <stdlib.h>
#define SIZE 102
void matmul(float** A, float** B, float** C, 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()
{
srand(time(NULL));
// 分配矩阵内存
float** A = (float**)malloc(SIZE * sizeof(float*));
float** B = (float**)malloc(SIZE * sizeof(float*));
float** C = (float**)malloc(SIZE * sizeof(float*));
for (int i = 0; i < SIZE; ++i)
{
A[i] = (float*)malloc(SIZE * sizeof(float));
B[i] = (float*)malloc(SIZE * sizeof(float));
C[i] = (float*)malloc(SIZE * sizeof(float));
}
//初始化矩阵数据
for (int i = 0; i < SIZE; i++)
{
for(int j=0;j<SIZE;j++)
{
A[i][j] = (float)(rand() % 100) / 100.0f;
B[i][j] = (float)(rand() % 100) / 100.0f;
}
}
clock_t start = clock();
matmul(A, B, C, SIZE);
clock_t end = clock();
// 计算并输出向量乘法的时间
double multiply_time_spent = double(end - start) / CLOCKS_PER_SEC;
printf("使用基础的向量乘法:\n当SIZE取%d时初始向量乘法时间%lf秒\n", SIZE,multiply_time_spent);
// 释放动态分配的内存
for (int i = 0; i < SIZE; ++i) {
free(A[i]);free(B[i]);free(C[i]);
}
free(A);free(B);free(C);
}