求指教:两个512阶float矩阵相乘,结果写入一个txt里边,运行之后文件大小不对。
两个512阶的单精度浮点数矩阵相乘,结果写入一个matrix.txt文件里边。
按说文件大小应该是512*512*4/1024=1024KB,结果我运行之后的结果却是5120KB,不懂,这是什么问题呢?
请大家帮忙,谢谢了
C++ C 矩阵
#include<iostream>
#include<time.h>
#include<windows.h>
#include<stdio.h>
using namespace std;
int main()
{
int t0,N;
DWORD dwGTCBegin, dwGTCEnd;
FILE *fp;
//int block,N;
if((fp=fopen("Matrix.txt","wt"))==NULL)
cout<<"error on open 'Matrix.txt'";
srand(0101);
cout<<"Please input the scale of matrix:";
cin>>N;
float *pMatrixA=new float[N*N];
float *pMatrixB=new float[N*N];
float *pMatrixC0=new float[N*N];
for(int i=0;i<N;i++)
for(int j=0;j<N;j++)
{
pMatrixA[N*i+j]=(float)rand();
pMatrixB[N*i+j]=(float)rand();
pMatrixC0[N*i+j]=0;
}
dwGTCBegin = GetTickCount();
for(int i=0;i<N;i++)
for(int j=0;j<N;j++)
for(int k=0;k<N;k++)
pMatrixC0[N*i+j]+=pMatrixA[i*N+k]*pMatrixB[k*N+j];
dwGTCEnd = GetTickCount();
t0=dwGTCEnd - dwGTCBegin;
printf("Serial code running time: %d\n", t0);
//fwrite(pMatrixC0,sizeof(float),N*N,fp); //把生成的数组矩阵存放入Matrix.txt文件
for(int i=0;i<N;i++)
for(int j=0;j<N;j++)
{
fprintf(fp,"%f ",pMatrixC0[i*N+j]);
}
fclose(fp);
delete[]pMatrixA,pMatrixB,pMatrixC0;
system("pause");
return 0;
}