1

我想寻求帮助来测量下面矩阵乘法的执行时间。我在 Windows 中运行代码。我尝试使用time.h,但无法测量。我必须把计时器放在哪里?

#include<stdio.h>
#include<math.h>
#include<time.h>

void main()
{
    int m1[10][10],i,j,k,m2[10][10],add[10][10],mult[10][10],r1,c1,r2,c2;
    /*double dif;
    time_t start, end;*/

    printf("Enter number of rows and columns of first matrix MAX 10\n");
    scanf("%d%d",&r1,&c1);
    printf("Enter number of rows and columns of second matrix MAX 10\n");
    scanf("%d%d",&r2,&c2);
    if(r2==c1)
    {
        printf("Enter rows and columns of First matrix \n");
        printf("Row wise\n");
        for(i=0;i<r1;i++)
        {
            for(j=0;j<c1;j++)
                scanf("%d",&m1[i][j]);
        }
        printf("You have entered the first matrix as follows:\n");
        for(i=0;i<r1;i++)
        {
            for(j=0;j<c1;j++)
                printf("%d\t",m1[i][j]);
            printf("\n");
        }
        printf("Enter rows and columns of Second matrix \n");
        printf("Again row wise\n");
        for(i=0;i<r2;i++)
        {
            for(j=0;j<c2;j++)
                scanf("%d",&m2[i][j]);
        }
        printf("You have entered the second matrix as follows:\n");
        for(i=0;i<r2;i++)
        {
            for(j=0;j<c2;j++)
                printf("%d\t",m2[i][j]);
            printf("\n");
        }
        /*time (&start);*/
        printf("Now we multiply both the above matrix \n");
        printf("The result of the multiplication is as follows:\n");
        /*a11xA11+a12xA21+a13xA31 a11xA12+a12xA22+a13xA32 a11xA13+a12xA23+a13xA33*/
        for(i=0;i<r1;i++)
        {
            for(j=0;j<c2;j++)
            {
                mult[i][j]=0;
                for(k=0;k<r1;k++)
                {
                    mult[i][j]+=m1[i][k]*m2[k][j];
                    /*mult[0][0]=m1[0][0]*m2[0][0]+m1[0][1]*m2[1][0]+m1[0][2]*m2[2][0];*/
                }
                printf("%d\t",mult[i][j]);
            }
            printf("\n");
            /*time (&end);
            dif (difftime (end, start);
            printf("Time of execution is : %f\n",dif)*/
        }
        getch();
    }
    else
    {
        printf("Matrix multiplication cannot be done");
    }
}

我希望测量尽可能准确。

4

3 回答 3

2

我认为在调用getch(). 这将使您有最大的机会计数超过 1 秒。要获得一个体面的度量,您可能需要多次重复整个乘法(以便总经过时间以 10 秒为单位测量)。您也应该避免在循环中打印;打印时间可能会主导计算时间。

剩下的麻烦是time()系统调用提供了 1 秒的时间分辨率。您确实需要具有亚秒级分辨率的计时例程,例如gettimeofday()(微秒)或clock_gettime()(纳秒)。请注意,分辨率和精度是不同的。(您可以改用clock()标准 C,但通常提供的分辨率要低得多。从历史上看,也可以使用ftime()并且times()可以使用。这些提供了毫秒分辨率。)在 Windows 上还有其他系统调用可用。您仍然需要大量重复计数以使计时有用(1000 次、10,000 次或 1,000,000 次),因为执行 10x10 矩阵乘法并不需要很长时间。

于 2012-05-28T05:28:51.743 回答
1

time 函数的精度,因此如果代码执行时间少于 1 秒,则无法获得正确的输出。在 Windows 上,我更喜欢使用GetTickCount

样本

#include "Windows.h"
int main (void)
{
  DWORD start,end;
  start = GetTickCount();
  //do something like Sleep(1000)
  end = GetTickCount();
  printf("elapse %d milliseconds\n", end - start);
  return 0;
}
于 2012-05-28T05:29:08.230 回答
0

怎么用clock()

#include <stdio.h>
#include <math.h>
#include <assert.h>
#include <time.h>

int main() {
 clock_t start, stop;
 double t = 0.0;

 /* Start timer */ 
 start = clock();    
 assert(start != -1);

 /* Perform calculations */

 /* Stop timer */
 stop = clock();
 t = (double) (stop-start)/CLOCKS_PER_SEC; 
 printf("Run time: %f\n", t);

 return(0);
} /* main */
于 2012-05-28T05:28:53.907 回答