zoukankan      html  css  js  c++  java
  • 矩阵加法 时间性能测量

    #include <stdio.h>
    #include <time.h>
    
    #define MAX_SIZE 100
    
    typedef struct 
    {
    	long repetitions;
    	double duration;
    }times;
    
    void timer(void (*add)( int a[][MAX_SIZE], int b[][MAX_SIZE],
    		   int c[][MAX_SIZE], int n),
    		   int a[][MAX_SIZE], int b[][MAX_SIZE],
    		   int c[][MAX_SIZE], int n, times *x);
    
    void add(int a[][MAX_SIZE], int b[][MAX_SIZE],
    		 int c[][MAX_SIZE], int n);
    
    		 
    int main(void)
    {
        static int a[MAX_SIZE][MAX_SIZE];
    //    static int b[MAX_SIZE][MAX_SIZE];
        static int c[MAX_SIZE][MAX_SIZE];
        
        int k=1;
        
    	times F;
        printf("function           repetitions   time");
    	for(int n=20; n<=MAX_SIZE; n+=20)
    	{
    		k=1;
    		for(int i=0; i<n; i++)
    		{
    			for(int j=0;j<n; j++)
    		    {
    		    	a[i][j]=k;
    //		    	b[i][j]=k;
    				k++;
    		    }
    		}
    //		for(int i=0; i<n; i++)
    //		    for(int j=0; j<n; j++)
    //		        {
    //		        	printf("%d ", a[i][j]);
    //		    	}		
    //		printf("
    ");
    		timer(add, a, a, c, n, &F);
    
    		printf("
    F: array_size:%3d  %d    %.15lf ", n, F.repetitions, F.duration);
    	}
        return 0;
    }
    
    void timer(void (*add_)( int a[][MAX_SIZE], int b[][MAX_SIZE],
    		   int c[][MAX_SIZE], int n),
    		   int a[][MAX_SIZE], int b[][MAX_SIZE],
    		   int c[][MAX_SIZE], int n, times *x)
    {
    	clock_t start=clock();
    	do{
    		x->repetitions++;
    		add_(a, b, c, n);
    	}while((clock()-start)<1000);
    	
    	x->duration=(double)(clock()-start)/CLOCKS_PER_SEC;
    	x->duration/=x->repetitions;
    }
    
    void add(int a[][MAX_SIZE], int b[][MAX_SIZE],
    		 int c[][MAX_SIZE], int n)
    {
    	for(int i=0; i<n; i++)
    		for(int j=0; j<n; j++)
    		    c[i][j]=a[i][j]+b[i][j];
    }
    

  • 相关阅读:
    2020年寒假假期总结0210
    2020年寒假假期总结0209
    2020年寒假假期总结0208
    2020年寒假假期总结0207
    2020年寒假假期总结0206
    yolo-v4:Optimal Speed and Accuracy of Object Detection解析
    Docker 练习
    tensorflow2.0 GPU版本镜像文件
    flink项目实战
    高等数理统计知识点
  • 原文地址:https://www.cnblogs.com/WALLACE-S-BOOK/p/9732304.html
Copyright © 2011-2022 走看看