我就知道一个
long start
long end
time(&start); time(&end); runtime=end-start
可是这个就显示几秒,太不准确了吧
大家帮忙告诉一个比较准确的计算时间方法吧
#include <time.h>
#include <stdio.h>
#include <windows.h>
#include <stdlib.h>
int main()
{
int a[8][8],b[8][8],c[8][8],d[8],m[8][8][8],n[8][8][8],p[8][8][8];
int i,j,k;
LARGE_INTEGER litmp1,litmp2;
LONGLONG QPart1,QPart2;
double dfMinus,dfFreq,dfTim1,dfTim2;
long l;
for (i=0;i<8;i++)
for (j=0;j<8;j++)
{
a[i][j]=rand()%100;
b[i][j]=rand()%200;
c[i][j]=0;
}
QueryPerformanceFrequency(&litmp1);
dfFreq=litmp1.QuadPart;
QueryPerformanceCounter(&litmp1);
QPart1=(double)litmp1.QuadPart;
for (l=0;l<50000;l++)
for (i=0;i<8;i++)
for (j=0;j<8;j++)
{
c[i][j]=0;
c[i][j]+=a[i][0]*b[0][j];
c[i][j]+=a[i][1]*b[1][j];
c[i][j]+=a[i][2]*b[2][j];
c[i][j]+=a[i][3]*b[3][j];
c[i][j]+=a[i][4]*b[4][j];
c[i][j]+=a[i][5]*b[5][j];
c[i][j]+=a[i][6]*b[6][j];
c[i][j]+=a[i][7]*b[7][j];
}
//添加需测试的代码
for (i=0;i<8;i++)
{
for (j=0;j<8;j++)
printf("%d ",c[i][j]);
printf("\n");
}
printf("\n");
QueryPerformanceCounter(&litmp1);
QPart2=litmp1.QuadPart;
dfMinus=(double)(QPart2-QPart1);
dfTim1=dfMinus/dfFreq;
printf("三乘三矩阵所用时间为:%f\n\n",dfTim1);
for (i=0;i<8;i++) //重新置0
for (j=0;j<8;j++)
c[i][j]=0;
/*----------------------流水工作--------------------------*/
QueryPerformanceFrequency(&litmp1);
dfFreq=litmp1.QuadPart;
QueryPerformanceCounter(&litmp1);
QPart1=(double)litmp1.QuadPart;
for (l=0;l<50000;l++)
{
for (i=0;i<8;i++)
for (j=0;j<8;j++)
{
d[0]=a[i][0]*b[0][j];
d[1]=a[i][1]*b[1][j];
d[2]=a[i][2]*b[2][j];
d[3]=a[i][3]*b[3][j];
d[4]=a[i][4]*b[4][j];
d[5]=a[i][5]*b[5][j];
d[6]=a[i][6]*b[6][j];
d[7]=a[i][7]*b[7][j];
d[0]=d[0]+d[1];
d[1]=d[2]+d[3];
d[2]=d[4]+d[5];
d[3]=d[6]+d[7];
d[0]=d[0]+d[1];
d[1]=d[2]+d[3];
d[0]=d[0]+d[1];
c[i][j]=d[0];
}
}
for (i=0;i<8;i++)
{
for (j=0;j<8;j++)
printf("%d ",c[i][j]);
printf("\n");
}
printf("\n");
QueryPerformanceCounter(&litmp1);
QPart2=litmp1.QuadPart;
dfMinus=(double)(QPart2-QPart1);
dfTim2=dfMinus/dfFreq;
printf("流水工作时间为:%f\n\n",dfTim2);
printf("两次时间之差为:%f\n\n",dfTim1-dfTim2);
return 0;
}
上次做流水实验用到的