English 中文(简体)
C 中的矩阵乘法如何计时?
原标题:How to time a matrix multiplication in C?
  • 时间:2012-05-28 05:21:42
  •  标签:
  • c
  • matrix

我想请求帮助测量下方矩阵乘法的执行时间。 我在 Windows 运行代码 。 我尝试使用 < a href=" http://www. cplusplus.com/ reference/clibrary/ ctime/ " relation="nofollow" >time. h < / a >, 但无法测量 。 我要将计时器放在哪里?

#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
");
    scanf("%d%d",&r1,&c1);
    printf("Enter number of rows and columns of second matrix MAX 10
");
    scanf("%d%d",&r2,&c2);
    if(r2==c1)
    {
        printf("Enter rows and columns of First matrix 
");
        printf("Row wise
");
        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:
");
        for(i=0;i<r1;i++)
        {
            for(j=0;j<c1;j++)
                printf("%d	",m1[i][j]);
            printf("
");
        }
        printf("Enter rows and columns of Second matrix 
");
        printf("Again row wise
");
        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:
");
        for(i=0;i<r2;i++)
        {
            for(j=0;j<c2;j++)
                printf("%d	",m2[i][j]);
            printf("
");
        }
        /*time (&start);*/
        printf("Now we multiply both the above matrix 
");
        printf("The result of the multiplication is as follows:
");
        /*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	",mult[i][j]);
            }
            printf("
");
            /*time (&end);
            dif (difftime (end, start);
            printf("Time of execution is : %f
",dif)*/
        }
        getch();
    }
    else
    {
        printf("Matrix multiplication cannot be done");
    }
}

我希望测量尽可能准确

问题回答

我认为,你最好不要在您目前掌握的循环之外,在拨打 gache () 之前使用结束时间代码。 这将给您最大的数数机会超过1秒。 要获得一个体面的尺度, 您可能需要重复整个乘法的多次( 以便用10秒的秒来测量总时间) 。 您也应该避免在循环中打印; 打印时间可能会主宰计算时间 。

您剩下的麻烦是 time () fear () < a href=\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

the time function s precision, so if the code executes less than 1 second, you can t get correct output. On windows, I prefer to use GetTickCount

样本样本样本

#include "Windows.h"
int main (void)
{
  DWORD start,end;
  start = GetTickCount();
  //do something like Sleep(1000)
  end = GetTickCount();
  printf("elapse %d milliseconds
", end - start);
  return 0;
}

使用 block () 怎么样?

#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
", t);

 return(0);
} /* main */




相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...