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.

55 lines
1.1 KiB

#include <stdio.h>
#include <stdlib.h>
#include <ctime>
#define 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++){
C[i][j]=0;
for(int k=0;k<n;k++){
C[i][j]+=A[i][k]*B[k][j];
}
}
}
}
int main(){
//分配矩阵内存
float** A=new float*[SIZE];
float** B=new float*[SIZE];
float** C=new float*[SIZE];
for(int i=0;i<SIZE;i++){
A[i]=new float[SIZE];
B[i]=new float[SIZE];
C[i]=new float[SIZE];
}
//初始化
srand(time(NULL));
for(int i=0;i<SIZE;i++){
for(int j=0;j<SIZE;j++){
A[i][j]=rand()%100;
B[i][j]=rand()%100;
}
}
//计时并输出
clock_t start=clock();
matmul(A,B,C,SIZE);
clock_t end=clock();
printf("初始稠密矩阵乘法时间:%f秒\n",(double)(end-start)/CLOCKS_PER_SEC);
for(int i=0;i<SIZE;i++){
delete[] A[i];
delete[] B[i];
delete[] C[i];
}
delete[] A;
delete[] B;
delete[] C;
return 0;
}